【问题标题】:How to lookup or aggregate a field of an object in an array of javascript objects (in mongodb)?如何在javascript对象数组(在mongodb中)中查找或聚合对象的字段?
【发布时间】:2021-04-27 10:12:14
【问题描述】:

我希望在我的 mongoose 架构中拥有字段 contractType,它是一个对象数组,每个对象都具有属性 authorizedBy 进入第二级。

contractType 字段可能有很多项目,我想知道每个 authorizedBy 在名为 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 个)而不是手动添加每个字段,例如 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 个)放在同一个对象中 :(

【问题讨论】:

  • 你能分享示例输入和预期输出吗?
  • 我用该信息更新问题:D

标签: mongodb lookup aggregation mongoose-schema unwind-segue


【解决方案1】:

你所做的几乎是正确的,

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 playground

【讨论】:

  • 但是如果在我的专业架构中我有 20 多个字段并且其中一些具有嵌入文档...有没有办法将所有属性传递给组的结果?像 javascript {...items} 中的扩展运算符
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-24
  • 2020-12-26
  • 2018-04-20
  • 1970-01-01
  • 2020-02-14
  • 2020-07-27
  • 1970-01-01
相关资源
最近更新 更多