如何在 MongoDB 中遍历数组并从特定集合中提取名称。

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

How to lookup through an Array in MongoDB and Project Names From a Certain Collection

问题

我有两个集合,一个叫做Exports,另一个叫做Service。在Exports集合中有一个对象,其中包含一个servicesIds数组。

我想要聚合和查找Exports集合中与Service集合匹配的_id,以查找服务的名称。

两个集合中每个文档的结构如下:

Exports:

{
    "_id" : "818a2c4fc4",
    "companyId" : "7feb1812d8",
    "filter" : {
        "servicesIds" : [
            "0111138dc679d",
            "0c18c499435e9",
        ],
       
    },
    "_created_at" : ISODate("2019-10-27T09:06:03.102+0000"),
    "_updated_at" : ISODate("2019-10-27T09:06:05.099+0000"),
}

Service:

{
    "_id" : "0111138dc679d",
    "name" : "Bay Services",
    "character" : "B",
    "company" : {
        "id" : "f718a1c385",
        "name" : "xxx"
    },
    "active" : true,
    "tags" : [

    ],
    "_created_at" : ISODate("2020-04-09T06:36:14.442+0000"),
    "_updated_at" : ISODate("2020-06-06T03:52:16.770+0000"),
}

我该如何做到这一点?

这是我尝试过的,但它一直给我一个错误:

Mongo服务器错误'$in需要一个数组作为第二个参数,找到:missing'。

这是我的代码:

db.getCollection("Exports").aggregate([
    {
        "$match": { "companyId": "818a2c4fc4" },
    },
    {
        "$lookup": {
            "from": "Service",
            "let": { id: "$_id" },
            "pipeline": [
                {
                    "$match": {
                        "$expr": {
                            "$in": ["$$id", "$filter.servicesIds"]
                        }
                    }
                }
            ],
            "as": "services"
        }
    },
])

(注意:由于您要求不翻译代码部分,我提供的是代码的直译翻译。)

英文:

I have two collections, one named Exports and one named Service. Inside the Exports collection there is an object that holds inside of it an array of servicesIds.

I want to aggregate and lookup for the corressponding matching _ids from the Exports collection with the Service collection to find the name of the services.

The structure of the each document for the two collection is as follows:

Exports:

{
    "_id" : "818a2c4fc4",
    "companyId" : "7feb1812d8",
    "filter" : {
        "servicesIds" : [
            "0111138dc679d",
            "0c18c499435e9",
        ],
       
    },
    "_created_at" : ISODate("2019-10-27T09:06:03.102+0000"),
    "_updated_at" : ISODate("2019-10-27T09:06:05.099+0000"),
}

Service:
An example of one document with its _id is a foreign key inside the filters object then inside the servicesIds array

{
    "_id" : "0111138dc679d",
    "name" : "Bay Services",
    "character" : "B",
    "company" : {
        "id" : "f718a1c385",
        "name" : "xxx"
    },
    "active" : true,
    "tags" : [

    ],
    "_created_at" : ISODate("2020-04-09T06:36:14.442+0000"),
    "_updated_at" : ISODate("2020-06-06T03:52:16.770+0000"),
}

How can i do that?

Here is what i tried, but it keeps giving me and error reading

Mongo Server error '$in requires an array as a second argument, found: missing' on server 

Here is my code:

db.getCollection("Exports").aggregate([
 
   {
       "$match": { "companyId":"818a2c4fc4" },
   },
   
   {
    "$lookup": {
    "from": "Service",
    "let":{ id : "$_id" },
    "pipeline": [
      {
          "$match": 
          {
          "$expr": 
              {
              "$in": ["$$id","$filter.servicesIds"]
              }
          }
      }
    ],
    "as":"services"
          }
      },
  
 ])

答案1

得分: 1

`$unwind` 首先展开数组,或者您可以编辑您的答案,提供您期望的结果,然后我会更正我的答案。

db.Exports.aggregate([
{
"$match": {
"companyId": "7feb1812d8"
}
},
{
"$unwind": "$filter.servicesIds"
},
{
"$lookup": {
"from": "Service",
"localField": "filter.servicesIds",
"foreignField": "_id",
"as": "docs"
}
}
])


https://mongoplayground.net/p/l2VweVYz1Fy
英文:

$unwind the array first, or you can edit your answer with an expected result you want, then I will correct my answer.

db.Exports.aggregate([
  {
    "$match": {
      "companyId": "7feb1812d8"
    }
  },
  {
    "$unwind": "$filter.servicesIds"
  },
  {
    "$lookup": {
      "from": "Service",
      "localField": "filter.servicesIds",
      "foreignField": "_id",
      "as": "docs"
    }
  }
])

https://mongoplayground.net/p/l2VweVYz1Fy

huangapple
  • 本文由 发表于 2023年2月16日 15:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469134.html
匿名

发表评论

匿名网友

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

确定