MongoDB match array based on document value [duplicate]

半腔热情 提交于 2021-01-29 13:16:45

问题


Let's say I have the following document structure:

{
  "A": {
    "_id": "ID0"
  },
  "B": [
    "a": {
      "_id": "ID0",
      "field": "X"
    },
    "b": {
      "_id": "ID1",
      "field": "Y"
    }
  ]
}

I want to project B matched with the _id in A. The end result would be:

{
  "B": [
    "a": {
      "_id": "ID0",
      "field": "X"
    }
  ]
}

I tried the following but apparently I'm doing something wrong:

db.collection.aggregate([
  {$match: {"B._id": "$A._id"}},
  {$project: {"B": 1}}
])

来源:https://stackoverflow.com/questions/61223895/mongodb-match-array-based-on-document-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!