首页 > 解决方案 > 如何在javascript对象数组(在mongodb中)中查找或聚合对象的字段?

问题描述

我想在我的猫鼬模式中有字段contractType,是一个对象数组,每个对象都有属性authorizedBy进入第二级。

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,
      },
    ),
  ],
)

这是我的管理员收藏。

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

我在professionalSchema mongodb compass中有这个:

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

这是管理员集合(adminSchema)的管理员:

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

我会在下面得到这样的响应,另外我想获取对象的所有字段(20 aprox)而不是手动添加每个字段,例如 javascript 中的扩展运算符(...item)

{
  "_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"
    }
  ]
}

我在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'
    }
  }
]

但我不知道如何将其余元素(大约 20 个)放在同一个对象中:(

标签: mongodblookupaggregationmongoose-schemaunwind-segue

解决方案


你所做的几乎是正确的,

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" }
    }
  }
])

工作Mongo游乐场


推荐阅读