首页 > 解决方案 > Mongo展开子对象和过滤器

问题描述

这是关于这个问题的 MongoPlayground

今天我有 2 个嵌套的对象数组:

对象模式

我有一系列会议,其中有一系列邀请。

我只需要投影会议,但邀请由 PartnerId 过滤。所以我有很多不同伙伴的邀请,但是当每个伙伴登录到系统时,他只能看到自己的邀请,而看不到其他人的邀请。换句话说,数组 Invites 将只有对它的 PartnerId 的邀请。

我可以进入项目部分,但过滤掉我丢失的邀请。我应该对另一个数组使用 $filter 或 $group 吗?

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

Mongo游乐场

db.collection.aggregate([
  {
    "$match": {
      "$nor": [
        {
          "Meetings": {
            "$exists": false
          }
        },
        {
          "Meetings": {
            "$size": 0.0
          }
        }
      ]
    }
  },
  {
    "$unwind": {
      "path": "$Meetings"
    }
  },
  {
    "$project": {
      "Meetings": "$Meetings"
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$Meetings"
    }
  }
])

回答我做了...

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
    }
);

标签: mongodbmongodb-query

解决方案


使用$filter是实现目标的好方法,您可以这样做:

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 "
            ]
          }
        }
      }
    }
  }
])

推荐阅读