title: How to lookup or aggregate a field of an object in an array of javascript objects (in mongodb)?
body: <p>I want have the field <strong>contractType</strong> in my mongoose schema, is an array of objects that each one has the property <strong>authorizedBy</strong> into the second level.</p>
  <p><strong>contractType</strong> field could have many items and I want to know each <strong>authorizedBy</strong> finding it in other collection called <strong>admins</strong> (I'll let it below too).</p>
  <pre><code>const professionalSchema = new Schema(
    firstName: String,
    contractType: [
      new Schema(
        {
          contract: String,
          authorizedBy: {
            type: ObjectId,
            ref: 'admin',
            index: true,
          },
          document: String
        },
        {
          timestamps: true,
          versionKey: false,
        },
      ),
    ],
  )
  </code></pre>
  <p>this is my admin collection.</p>
  <pre><code>const adminSchema = new Schema(
    {
      firstName: String,
      lastName: String,
      role: String
    },
    {
      timestamps: true,
      versionKey: false,
    },
  )
  </code></pre>
  <p>I have this in <strong>professionalSchema</strong> mongodb compass:</p>
  <pre><code>{
    &quot;_id&quot;: ObjectId(&quot;6009a0d0874f0900086ee0ce&quot;),
    &quot;firstName&quot;: &quot;xxxx&quot;,
    &quot;gender&quot;: &quot;NOT_SPECIFIED&quot;,
    &quot;contractType&quot;: [
      {
        &quot;authorizedBy&quot;: ObjectId(&quot;5fad90665d963cbbbd4a6580&quot;),
        &quot;document&quot;: &quot;document 1&quot;
      },
      {
        &quot;authorizedBy&quot;: ObjectId(&quot;5fad90665d963cbbbd4a6580&quot;),
        &quot;document&quot;: &quot;document 2&quot;
      }
    ]
  }
  </code></pre>
  <p>this is an admin of the collection of admins (<strong>adminSchema</strong>):</p>
  <pre><code>{
    &quot;_id&quot;: ObjectId(&quot;5fad90665d963cbbbd4a6580&quot;),
    &quot;role&quot;: &quot;SAM&quot;,
    &quot;firstName&quot;: &quot;firstname&quot;,
    &quot;lastName&quot;: &quot;lastname&quot;
  }
  </code></pre>
  <p><strong>I would have response like this one below, additionally I want to get all fields of the object (20 aprox) not adding each one manually, like spread operator (...item) in javascript</strong></p>
  <pre><code>{
    &quot;_id&quot;: ObjectId(&quot;6009a0d0874f0900086ee0ce&quot;),
    &quot;firstName&quot;: &quot;xxxx&quot;,
    &quot;contractType&quot;: [
      {
        &quot;authorizedBy&quot;: {
          &quot;_id&quot;: &quot;5fad90665d963cbbbd4a6580&quot;,
          &quot;firstName&quot;: &quot;firstname&quot;,
          &quot;lastName&quot;: &quot;lastname&quot;,
          &quot;role&quot;: &quot;SAM&quot;
        },
        &quot;document&quot;: &quot;document 1&quot;
      },
      {
        &quot;authorizedBy&quot;: {
          &quot;_id&quot;: &quot;5fad90665d963cbbbd4a6580&quot;,
          &quot;firstName&quot;: &quot;firstname&quot;,
          &quot;lastName&quot;: &quot;lastname&quot;,
          &quot;role&quot;: &quot;SAM&quot;
        },
        &quot;document&quot;: &quot;document 2&quot;
      }
    ]
  }
  </code></pre>
  <p>I tried this in mongo compass.</p>
  <pre><code>[
    {
      '$match': {
        '_id': new ObjectId('6009a0d0874f0900086ee0ce')
      }
    }, {
      '$unwind': {
        'path': '$contractType'
      }
    }, {
      '$lookup': {
        'from': 'admins', 
        'localField': 'contractType.authorizedBy', 
        'foreignField': '_id', 
        'as': 'contractType.authorizedBy'
      }
    }, {
      '$unwind': {
        'path': '$contractType.authorizedBy'
    }
    }, {
      $group': {
        '_id': '$_id'
      }
    }
  ]
  </code></pre>
  <p><strong>but I don't know how put rest of elements (20 aprox) in the same object :(</strong></p>