简体   繁体   English

如何 mongodb 搜索嵌套文档并找到完全匹配的字段

[英]how to mongodb search nested documents and find exact match field

records of collections collections 的记录

i have used the below code to find but it showing all the accounts data我已使用以下代码查找,但它显示了所有帐户数据

db.getCollection('leads').find({"accounts.status":"suspended"})

after search query the result搜索后查询结果

i want to search where accounts.status=suspended but the result should show match nested documents under accounts我想搜索accounts.status=suspended但结果应该在accounts下显示匹配的嵌套文档

use $set with $filter after $match$match之后使用$set$filter

db.collection.aggregate([
  {
    "$match": {
      "accounts.status": "suspended"
    }
  },
  {
    "$set": {
      "accounts": {
        "$filter": {
          "input": "$accounts",
          "as": "a",
          "cond": {
            $eq: [
              "$$a.status",
              "suspended"
            ]
          }
        }
      }
    }
  }
])

mongoplayground mongoplayground

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM