嵌套数组的MongoDB投影

时间:2015-03-11 09:04:19

标签: mongodb mongodb-query aggregation-framework

我有一个集合“帐户”,其中包含类似于此结构的文档:

{
    "email" : "john.doe@acme.com",
    "groups" : [
        {
            "name" : "group1",
            "contacts" : [
                { "localId" : "c1", "address" : "some address 1" },
                { "localId" : "c2", "address" : "some address 2" },
                { "localId" : "c3", "address" : "some address 3" }
            ]
        },
        {
            "name" : "group2",
            "contacts" : [
                { "localId" : "c1", "address" : "some address 1" },
                { "localId" : "c3", "address" : "some address 3" }
            ]
        }
    ]
}

q = { "email" : "john.doe@acme.com", "groups" : { $elemMatch: { "name" : "group1" } } }
p = { "groups.name" : 0, "groups" : { $elemMatch: { "name" : "group1" } } }
db.accounts.find( q, p ).pretty()

我将成功获得我感兴趣的指定帐户的组。

问题:如何在指定“帐户”的特定“群组”中获取有限的“联系人”列表?我们假设我有以下论点:

  • 帐号:电子邮件 - “john.doe@acme.com”
  • group:name - “group1”
  • contact:localIds数组 - [“c1”,“c3”,“Not existing id”]

鉴于这些论点,我希望得到以下结果:

{
    "groups" : [
        {
            "name" : "group1", (might be omitted)
            "contacts" : [
                { "localId" : "c1", "address" : "some address 1" },
                { "localId" : "c3", "address" : "some address 3" }
            ]
        }
    ]
}

除了最终的联系人之外,我不需要任何其他内容。

途径

为简单起见,所有查询都尝试仅提取一个匹配的联系人而不是匹配的联系人列表。 我尝试了以下查询但没有取得任何成功:

p = { "groups.name" : 0, "groups" : { $elemMatch: { "name" : "group1", "contacts" : { $elemMatch: { "localId" : "c1" } } } } }
p = { "groups.name" : 0, "groups" : { $elemMatch: { "name" : "group1", "contacts.localId" : "c1" } } }
not working: returns whole array or nothing depending on localId


p = { "groups.$" : { $elemMatch: { "localId" : "c1" } } }
error: {
    "$err" : "Can't canonicalize query: BadValue Cannot use $elemMatch projection on a nested field.",
    "code" : 17287
}


p = { "groups.contacts" : { $elemMatch: { "localId" : "c1" } } }
error: {
    "$err" : "Can't canonicalize query: BadValue Cannot use $elemMatch projection on a nested field.",
    "code" : 17287
}

感谢任何帮助!

2 个答案:

答案 0 :(得分:34)

2017年更新

这样一个好问题值得现代回应。所请求的数组过滤实际上可以在3.2之后的现代MongoDB版本中通过简单的$match$project管道阶段完成,就像原始的普通查询操作一样。

db.accounts.aggregate([
  { "$match": {
    "email" : "john.doe@acme.com",
    "groups": {
      "$elemMatch": { 
        "name": "group1",
        "contacts.localId": { "$in": [ "c1","c3", null ] }
      }
    }
  }},
  { "$addFields": {
    "groups": {
      "$filter": {
        "input": {
          "$map": {
            "input": "$groups",
            "as": "g",
            "in": {
              "name": "$$g.name",
              "contacts": {
                "$filter": {
                  "input": "$$g.contacts",
                  "as": "c",
                  "cond": {
                    "$or": [
                      { "$eq": [ "$$c.localId", "c1" ] },
                      { "$eq": [ "$$c.localId", "c3" ] }
                    ]
                  } 
                }
              }
            }
          }
        },
        "as": "g",
        "cond": {
          "$and": [
            { "$eq": [ "$$g.name", "group1" ] },
            { "$gt": [ { "$size": "$$g.contacts" }, 0 ] }
          ]
        }
      }
    }
  }}
])

这使得$filter$map运算符只能在符合条件的情况下返回数组中的元素,并且性能远远优于使用$unwind。由于管道阶段有效地反映了.find()操作中“查询”和“项目”的结构,因此这里的性能与此类操作基本相同。

请注意,如果目的是实际使用“跨文档”将详细信息从“多个”文档而不是“一个”中提取出来,那么这通常需要某种类型的{{1为了这样做,操作使得数组项可以被“分组”访问。


这基本上是方法:

$unwind

这是多个匹配的“数组过滤”,db.accounts.aggregate([ // Match the documents by query { "$match": { "email" : "john.doe@acme.com", "groups.name": "group1", "groups.contacts.localId": { "$in": [ "c1","c3", null ] }, }}, // De-normalize nested array { "$unwind": "$groups" }, { "$unwind": "$groups.contacts" }, // Filter the actual array elements as desired { "$match": { "groups.name": "group1", "groups.contacts.localId": { "$in": [ "c1","c3", null ] }, }}, // Group the intermediate result. { "$group": { "_id": { "email": "$email", "name": "$groups.name" }, "contacts": { "$push": "$groups.contacts" } }}, // Group the final result { "$group": { "_id": "$_id.email", "groups": { "$push": { "name": "$_id.name", "contacts": "$contacts" }} }} ]) 的基本投影功能无法做到。

您有“嵌套”数组,因此需要处理$unwind两次。与其他操作一起。

答案 1 :(得分:3)

您可以使用聚合框架的$unwind运算符。 例如:

db.contact.aggregate({$unwind:'$groups'}, {$unwind:'$groups.contacts'}, {$match:{email:'john.doe@acme.com', 'groups.name':'group1', 'groups.contacts.localId':{$in:['c1', 'c3', 'whatever']}}});

应该给出以下结果:

{ "_id" : ObjectId("5500103e706342bc096e2e14"), "email" : "john.doe@acme.com", "groups" : { "name" : "group1", "contacts" : { "localId" : "c1", "address" : "some address 1" } } }
{ "_id" : ObjectId("5500103e706342bc096e2e14"), "email" : "john.doe@acme.com", "groups" : { "name" : "group1", "contacts" : { "localId" : "c3", "address" : "some address 3" } } }

如果您只想要一个对象,则可以使用$group运算符。

相关问题