简体   繁体   English

如何在 javascript 对象数组(在 mongodb 中)中查找或聚合 object 的字段?

[英]How to lookup or aggregate a field of an object in an array of javascript objects (in mongodb)?

I want have the field contractType in my mongoose schema, is an array of objects that each one has the property authorizedBy into the second level.我想在我的 mongoose 模式中拥有字段contractType ,它是一个对象数组,每个对象都具有属性authorizedBy进入第二级。

contractType field could have many items and I want to know each authorizedBy finding it in other collection called admins (I'll let it below too). contractType字段可能有很多项目,我想知道每个授权通过在称为admins的其他集合中找到它(我也会让它在下面)。

const professionalSchema = new Schema(
  firstName: String,
  contractType: [
    new Schema(
      {
        contract: String,
        authorizedBy: {
          type: ObjectId,
          ref: 'admin',
          index: true,
        },
        document: String
      },
      {
        timestamps: true,
        versionKey: false,
      },
    ),
  ],
)

this is my admin collection.这是我的管理员收藏。

const adminSchema = new Schema(
  {
    firstName: String,
    lastName: String,
    role: String
  },
  {
    timestamps: true,
    versionKey: false,
  },
)

I have this in professionalSchema mongodb compass:我在专业模式 mongodb 指南针中有这个:

{
  "_id": ObjectId("6009a0d0874f0900086ee0ce"),
  "firstName": "xxxx",
  "gender": "NOT_SPECIFIED",
  "contractType": [
    {
      "authorizedBy": ObjectId("5fad90665d963cbbbd4a6580"),
      "document": "document 1"
    },
    {
      "authorizedBy": ObjectId("5fad90665d963cbbbd4a6580"),
      "document": "document 2"
    }
  ]
}

this is an admin of the collection of admins ( adminSchema ):这是管理员集合( adminSchema )的管理员:

{
  "_id": ObjectId("5fad90665d963cbbbd4a6580"),
  "role": "SAM",
  "firstName": "firstname",
  "lastName": "lastname"
}

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我会在下面得到这样的响应,另外我想获取 object(20 aprox)的所有字段而不是手动添加每个字段,例如 javascript 中的扩展运算符(...项目)

{
  "_id": ObjectId("6009a0d0874f0900086ee0ce"),
  "firstName": "xxxx",
  "contractType": [
    {
      "authorizedBy": {
        "_id": "5fad90665d963cbbbd4a6580",
        "firstName": "firstname",
        "lastName": "lastname",
        "role": "SAM"
      },
      "document": "document 1"
    },
    {
      "authorizedBy": {
        "_id": "5fad90665d963cbbbd4a6580",
        "firstName": "firstname",
        "lastName": "lastname",
        "role": "SAM"
      },
      "document": "document 2"
    }
  ]
}

I tried this in mongo compass.我在mongo compass中试过这个。

[
  {
    '$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'
    }
  }
]

but I don't know how put rest of elements (20 aprox) in the same object:(但我不知道如何将 rest 元素(大约 20 个)放在同一个 object 中:(

What you have done is almost correct,你所做的几乎是正确的,

db.professionalSchema.aggregate([
  {"$match": {"_id": ObjectId("6009a0d0874f0900086ee0ce") } },
  {"$unwind": {"path": "$contractType" }},
  {
    "$lookup": {
      "from": "adminSchema",
      "localField": "contractType.authorizedBy",
      "foreignField": "_id",
      "as": "contractType.authorizedBy"
    }
  },
  {
    "$addFields": {
      "contractType.authorizedBy": {
        $ifNull: [
          {  $arrayElemAt: [ "$contractType.authorizedBy", 0 ] },
          ""
        ]
      }
    }
  },
  {
    "$group": {
      "_id": "$_id",
      "firstName": { $first: "$firstName" },
      "gender": { $first: "$gender"},
      contractType: { $push: "$contractType" }
    }
  }
])

Working Mongo playground工作蒙戈游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM