首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongo解开子对象和滤镜

问Mongo解开子对象和滤镜
EN

Stack Overflow用户
提问于 2020-12-12 04:22:52
回答 1查看 72关注 0票数 0

关于这个问题的Here is the MongoPlayground

今天我有两个嵌套的object数组:

​

​

我有一系列的会议,里面有一系列的邀请。

我只需要项目的会议,但与PartnerId过滤的邀请。因此,我有许多不同合作伙伴的邀请,但当每个合作伙伴登录系统时,他只能看到自己的邀请,而不是其他人的邀请。换句话说,数组Invites将只有invites到它的PartnerId。

我可以到达项目部分,但过滤邀请我迷路了。我应该对另一个阵列使用$filter或$group吗?

这是我到目前为止所得到的:

MongoPlayground

代码语言:javascript
复制
db.collection.aggregate([
  {
    "$match": {
      "$nor": [
        {
          "Meetings": {
            "$exists": false
          }
        },
        {
          "Meetings": {
            "$size": 0.0
          }
        }
      ]
    }
  },
  {
    "$unwind": {
      "path": "$Meetings"
    }
  },
  {
    "$project": {
      "Meetings": "$Meetings"
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$Meetings"
    }
  }
])

回答说我做到了..。

代码语言:javascript
复制
db.getCollection("ClientProject").aggregate(
    [
        { 
            "$match" : { 
                "$and" : [
                    { 
                        "PartnerIds" : { 
                            "$in" : [
                                "5f9b247f0ca60a000232cacf"
                            ]
                        }
                    }, 
                    { 
                        "$nor" : [
                            { 
                                "Meetings" : { 
                                    "$exists" : false
                                }
                            }, 
                            { 
                                "Meetings" : { 
                                    "$size" : 0.0
                                }
                            }, 
                            { 
                                "Meetings" : { 
                                    "$eq" : null
                                }
                            }
                        ]
                    }
                ]
            }
        }, 
        { 
            "$unwind" : { 
                "path" : "$Meetings"
            }
        }, 
        { 
            "$project" : { 
                "Meetings" : "$Meetings"
            }
        }, 
        { 
            "$replaceRoot" : { 
                "newRoot" : "$Meetings"
            }
        }, 
        { 
            "$addFields" : { 
                "Invites" : { 
                    "$filter" : { 
                        "input" : "$Invites", 
                        "as" : "invite", 
                        "cond" : { 
                            "$eq" : [
                                "$$invite.PartnerId", 
                                "5f9b247f0ca60a000232cacf"
                            ]
                        }
                    }
                }
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);
EN

回答 1

Stack Overflow用户

发布于 2020-12-12 05:26:43

使用$filter是实现您的目标的好方法,下面是您如何做到这一点:

代码语言:javascript
复制
db.collection.aggregate([
  {
    "$match": {
      "$nor": [
        {
          "Meetings": {
            "$exists": false
          }
        },
        {
          "Meetings": {
            "$size": 0.0
          }
        }
      ]
    }
  },
  {
    "$unwind": {
      "path": "$Meetings"
    }
  },
  {
    "$project": {
      "invites": {
        "$filter": {
          "input": "$Meetings.Invites",
          "as": "invite",
          "cond": {
            "$eq": [
              "$$invite.PartnerName",
              "Thiago Parceiro "
            ]
          }
        }
      }
    }
  }
])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65258137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档