mongodb中的过滤器嵌套数组?

mongodb中的过滤器嵌套数组?,mongodb,mongoose,mongodb-query,aggregation-framework,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我有一份文件看起来是这样的: { "_id": { "$oid": "5b1586ccf0c56353e89d330b" }, "address": { "street": "123 Street", "address2": "Address 2", "city": "Some City", "state": "MI", "zip": "12345" }, "s

我有一份文件看起来是这样的:

{
    "_id": {
        "$oid": "5b1586ccf0c56353e89d330b"
    },
    "address": {
        "street": "123 Street",
        "address2": "Address 2",
        "city": "Some City",
        "state": "MI",
        "zip": "12345"
    },
    "subs": [
        {
            "invoices": [
                {
                    "address": {
                        "street": "3061 Pine Ave SW",
                        "city": "Grandville",
                        "state": "AK",
                        "zip": "49418"
                    },
                    "lineItem": [
                        {
                            "images": [
                                {
                                    "_id": {
                                        "$oid": "5b1fca54e6ee1d80c463612d"
                                    },
                                    "name": "1528810066348_RSA Logo.jpeg",
                                    "url": "https....",
                                    "uploadDate": {
                                        "$date": "2018-06-12T13:27:46.931Z"
                                    },
                                    "size": 91819
                                }
                            ],
                            "_id": {
                                "$oid": "5b1fca54e6ee1d80c463612c"
                            },
                            "desc": "2",
                            "amt": 2
                        }
                    ],
                    "_id": {
                        "$oid": "5b1fca54e6ee1d80c463612b"
                    }
                }
            ],
            "_id": {
                "$oid": "5b1fc7f23b595481d4599f58"
            },
            "email": "a@a.com",
            "scope": "Roof",
        },
        {
            "invoices": [
                {
                    "address": {
                        "street": "3061 Pine Ave SW",
                        "city": "Grandville",
                        "state": "AL",
                        "zip": "49418"
                    },
                    "lineItem": [
                        {
                            "images": [
                                {
                                    "_id": {
                                        "$oid": "5b1fca2fe6ee1d80c463612a"
                                    },
                                    "name": "1528810029700_RSA Stamp.png",
                                    "url": "https....",
                                    "uploadDate": {
                                        "$date": "2018-06-12T13:27:10.403Z"
                                    },
                                    "size": 238113
                                }
                            ],
                            "_id": {
                                "$oid": "5b1fca2fe6ee1d80c4636129"
                            },
                            "desc": "1",
                            "amt": 1
                        }
                    ],
                    "_id": {
                        "$oid": "5b1fca2fe6ee1d80c4636128"
                    }
                },
                {
                    "address": {
                        "street": "3061 Pine Ave SW",
                        "city": "Grandville",
                        "state": "AL",
                        "zip": "49418"
                    },
                    "lineItem": [
                        {
                            "images": [
                                {
                                    "_id": {
                                        "$oid": "5b1fd05b0d1f7185e02e9c40"
                                    },
                                    "name": "1528811607099_error page.PNG",
                                    "url": "https....",
                                    "uploadDate": {
                                        "$date": "2018-06-12T13:53:28.080Z"
                                    },
                                    "size": 224772
                                }
                            ],
                            "_id": {
                                "$oid": "5b1fd05b0d1f7185e02e9c3f"
                            },
                            "desc": "3",
                            "amt": 3
                        }
                    ],
                    "_id": {
                        "$oid": "5b1fd05b0d1f7185e02e9c3e"
                    }
                }
            ],
            "_id": {
                "$oid": "5b1fc7f23b595481d4599f55"
            },
            "email": "b@b.com",
            "scope": "Siding",
        }
    ],
    "firstName": "",
    "lastName": "",
}
我的问题是,我希望能够访问特定
子系统的特定
发票

我是Mongo/Mongoose的新手,所以我可能做了一些完全错误的事情,对于我如何处理这件事的任何回答/批评,我都会非常高兴

--扭曲的答案--


你可以试试下面的聚合

这是一个使用解构数组和使用重建数组的漫长过程

或者您也可以通过聚合来实现这一点

db.collection.aggregate([
  { "$match": { "_id": "5b1586ccf0c56353e89d330b" }},
  { "$unwind": "$subs" },
  { "$match": { "subs._id": "5b1fc7f23b595481d4599f58" }},
  { "$project": {
    "address": 1, "firstName": 1, "lastName": 1,
    "subs.type": "$subs._id",
    "subs.status": "$subs.email",
    "subs.code": "$subs.scope",
    "subs.invoices": {
      "$filter": {
        "input": "$subs.invoices",
        "as": "invoice",
        "cond": {
          "$eq": [
            "$$invoice._id",
            "5b1fca54e6ee1d80c463612b"
          ]
        }
      }
    }
  }},
  { "$group": {
    "_id": "$_id",
    "address": { "$first": "$address" },
    "firstName": { "$first": "$firstName" },
    "lastName": { "$first": "$lastName" },
    "subs": { "$push": "$subs" }
  }}
])

谢谢你的快速回复!这仍然会返回所有发票,而不仅仅是与发票匹配的发票id@CraigHowell你能发布你的样本集吗样本集已经发布了added@CraigHowell请检查更新后的答案是否仍在使用您的答案,但最初返回的是一个空白数组。我相信所有ID都能正确通过。
db.collection.aggregate([
  { "$match": { "_id": "1111" } },
  { "$unwind": "$subs" },
  { "$match": { "subs._id": "2222" } },
  { "$unwind": "$subs.invoices" },
  { "$match": { "subs.invoices._id": "3333" } },
  { "$group": {
    "_id": {
      "_id": "$_id",
      "subs": "$subs._id"
    },
    "firstName": { "$first": "$firstName" },
    "lastName": { "$first": "$lastName" },
    "address": { "$first": "$address" },
    "subs": {
      "$first": {
        "_id": "$subs._id",
        "email": "$subs.email",
        "venue": "$subs.venue",
        "scope": "$subs.scope"
      }
    },
    "invoices": { "$push": "$subs.invoices" }
  }},
  { "$group": {
    "_id": "$_id._id",
    "firstName": { "$first": "$firstName" },
    "lastName": { "$first": "$lastName" },
    "address": { "$first": "$address" },
    "subs": {
      "$push": {
        "_id": "$subs._id",
        "email": "$subs.email",
        "venue": "$subs.venue",
        "scope": "$subs.scope",
        "invoices": "$invoices"
      }
    }
  }}
])
db.collection.aggregate([
  { "$match": { "_id": "5b1586ccf0c56353e89d330b" }},
  { "$unwind": "$subs" },
  { "$match": { "subs._id": "5b1fc7f23b595481d4599f58" }},
  { "$project": {
    "address": 1, "firstName": 1, "lastName": 1,
    "subs.type": "$subs._id",
    "subs.status": "$subs.email",
    "subs.code": "$subs.scope",
    "subs.invoices": {
      "$filter": {
        "input": "$subs.invoices",
        "as": "invoice",
        "cond": {
          "$eq": [
            "$$invoice._id",
            "5b1fca54e6ee1d80c463612b"
          ]
        }
      }
    }
  }},
  { "$group": {
    "_id": "$_id",
    "address": { "$first": "$address" },
    "firstName": { "$first": "$firstName" },
    "lastName": { "$first": "$lastName" },
    "subs": { "$push": "$subs" }
  }}
])