如何使用MongoDB聚合替换包含ID的对象数组中的数据

huangapple go评论66阅读模式
英文:

How to replace array of object containing ids with the data using MongoDB aggregation

问题

我有一个包含以下数据的集合,并希望得到下面我提到的理想输出。

db={
  collectionA: [
    {
      "id": ObjectId("63b7c24c06ebe7a8fd11777b"),
      "uniqueRefId": "UUID-2023-0001",
      "products": [
        {
          "productIndex": 1,
          "productCategory": ObjectId("63b7c24c06ebe7a8fd11777b"),
          "productOwners": [
            ObjectId("63b7c2fd06ebe7a8fd117781")
          ]
        },
        {
          "productIndex": 2,
          "productCategory": ObjectId("63b7c24c06ebe7a8fd11777b"),
          "productOwners": [
            ObjectId("63b7c2fd06ebe7a8fd117781"),
            ObjectId("63b7c12706ebe7a8fd117778")
          ]
        },
        {
          "productIndex": 3,
          "productCategory": "",
          "productOwners": ""
        }
      ]
    }
  ],
  collectionB: [
    {
      "_id": ObjectId("63b7c2fd06ebe7a8fd117781"),
      "fullname": "Jim Corbett",
      "email": "jim.corbett@pp.com"
    },
    {
      "_id": ObjectId("63b7c12706ebe7a8fd117778"),
      "fullname": "Carry Minatti",
      "email": "carry.minatty@pp.com"
    },
    
  ]
}

Desirable Output = [
    {
        "id": ObjectId("507f1f77bcf86cd799439011"),
        "uniqueRefId": "UUID-2023-0001",
        "products": [
            {
                "productIndex": 1,
                "productCategory": ObjectId('614g2f77bff86cd755439021'),
                "productOwners": [
                    {
                        "_id": ObjectId("63ac1e59c0afb8b6f2d41acd"),
                        "fullname": "Jim Corbett",
                        "email": "jim.corbett@pp.com"
                    }
                ]
            },
            {
                "productIndex": 2,
                "productCategory": ObjectId('614g2f77bff86cd755439021'),
                "productOwners": [
                    {
                        "_id": ObjectId("63ac1e59c0afb8b6f2d41acd"),
                        "fullname": "Jim Corbett",
                        "email": "jim.corbett@pp.com"
                    },
                    {
                        "_id": ObjectId("63ac1e59c0afb8b6f2d41ace"),
                        "fullname": "Carry Minatti",
                        "email": "carry.minatty@pp.com"
                    }
                ]
            },
            {
                "productIndex": 3,
                "productCategory": "",
                "productOwners": ""
            }
        ]
    }
]

在collectionA中,我们还有其他文档,不仅仅是一个文档。collectionB也有其他文档。

我们如何可以获得这个理想的输出?

我期望得到用于获得这个解决方案的MongoDB查询。

我已经实现了类似以下的lookup:

db.collectionA.aggregate([
  {
    "$lookup": {
      "from": "collectionB",
      "localField": "products.productOwners",
      "foreignField": "_id",
      "as": "inventory_docs"
    }
  }
])
英文:

I am having a collection which contains the data like the following and want to have the desirable output which I have mentioned below.

db={
collectionA: [
{
"id": ObjectId("63b7c24c06ebe7a8fd11777b"),
"uniqueRefId": "UUID-2023-0001",
"products": [
{
"productIndex": 1,
"productCategory": ObjectId("63b7c24c06ebe7a8fd11777b"),
"productOwners": [
ObjectId("63b7c2fd06ebe7a8fd117781")
]
},
{
"productIndex": 2,
"productCategory": ObjectId("63b7c24c06ebe7a8fd11777b"),
"productOwners": [
ObjectId("63b7c2fd06ebe7a8fd117781"),
ObjectId("63b7c12706ebe7a8fd117778")
]
},
{
"productIndex": 3,
"productCategory": "",
"productOwners": ""
}
]
}
],
collectionB: [
{
"_id": ObjectId("63b7c2fd06ebe7a8fd117781"),
"fullname": "Jim Corbett",
"email": "jim.corbett@pp.com"
},
{
"_id": ObjectId("63b7c12706ebe7a8fd117778"),
"fullname": "Carry Minatti",
"email": "carry.minatty@pp.com"
},
]
}
Desirable Output = [
{
"id": ObjectId("507f1f77bcf86cd799439011"),
"uniqueRefId": "UUID-2023-0001",
"products": [
{
"productIndex": 1,
"productCategory": ObjectId('614g2f77bff86cd755439021'),
"productOwners": [
{
"_id": ObjectId("63ac1e59c0afb8b6f2d41acd"),
"fullname": "Jim Corbett",
"email": "jim.corbett@pp.com"
}
]
},
{
"productIndex": 2,
"productCategory": ObjectId('614g2f77bff86cd755439021'),
"productOwners": [
{
"_id": ObjectId("63ac1e59c0afb8b6f2d41acd"),
"fullname": "Jim Corbett",
"email": "jim.corbett@pp.com"
},
{
"_id": ObjectId("63ac1e59c0afb8b6f2d41ace"),
"fullname": "Carry Minatti",
"email": "carry.minatty@pp.com"
}
]
},
{
"productIndex": 3,
"productCategory": "",
"productOwners": ""
}
]
}
]

In the collectionA we are having other documents as well, its not just one document.
Similarly for collectionB we are having other documents too.

How we can get this desirable output?

I am expecting the mongodb query for getting this solution.

I have implemented the lookup like the following

db.collectionA.aggregate([
{
"$lookup": {
"from": "collectionB",
"localField": "products.productOwners",
"foreignField": "_id",
"as": "inventory_docs"
}
}
])

答案1

得分: 0

你可以尝试这样做:

db.collectionA.aggregate([
  {
    "$unwind": "$products"
  },
  {
    "$lookup": {
      "from": "collectionB",
      "localField": "products.productOwners",
      "foreignField": "_id",
      "as": "products.productOwners"
    }
  },
  {
    "$group": {
      "_id": {
        id: "$id",
        uniqueRefId: "$uniqueRefId"
      },
      "products": {
        "$push": "$products"
      }
    }
  },
  {
    "$project": {
      id: "$_id.id",
      uniqueRefId: "$_id.uniqueRefId",
      products: 1,
      _id: 0
    }
  }
])

Playground链接

在这个查询中,我们执行以下操作:

  1. 首先,使用 $unwind 展开 products 数组。
  2. 然后,使用 $lookup 计算 productOwners
  3. 接下来,使用 $group 对展开的元素进行分组。
  4. 最后,使用 $project 投影所需的输出。
英文:

You can try this:

db.collectionA.aggregate([
{
"$unwind": "$products"
},
{
"$lookup": {
"from": "collectionB",
"localField": "products.productOwners",
"foreignField": "_id",
"as": "products.productOwners"
}
},
{
"$group": {
"_id": {
id: "$id",
uniqueRefId: "$uniqueRefId"
},
"products": {
"$push": "$products"
}
}
},
{
"$project": {
id: "$_id.id",
uniqueRefId: "$_id.uniqueRefId",
products: 1,
_id: 0
}
}
])

Playground link.

In this query, we do the following:

  1. First we unwind the products array, using $unwind.
  2. Then we calculate productOwners, using $lookup.
  3. Then we group the unwinded elements, using $group.
  4. Finally we, project the desired output using $project.

huangapple
  • 本文由 发表于 2023年1月6日 13:14:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定