mongodb如何对文档内数组进行过滤的方法步骤

本文介绍了mongodb如何对文档内数组进行过滤的方法步骤,分享给大家,具体如下:


mongodb文档内包含数组,需要将数组中符合条件的数据过滤出来并返回结果集,可以用两种方式来查询group或filter。
数据源:

{
  "_id" : ObjectId("5bbcc0c9a74db9804e78a157"),
  "uid" : "1000001",
  "name" : "zhangsan",
  "addrs" : [ 
    {
      "is_query" : "1",
      "city" : "北京"
    }, 
    {
      "is_query" : "0",
      "city" : "上海"
    }, 
    {
      "is_query" : "1",
      "city" : "深圳"
    }
  ]
}
{
  "_id" : ObjectId("5bbcc167a74db9804e78a172"),
  "uid" : "1000002",
  "name" : "lisi",
  "addrs" : [ 
    {
      "is_query" : "0",
      "city" : "北京"
    }, 
    {
      "is_query" : "0",
      "city" : "上海"
    }, 
    {
      "is_query" : "1",
      "city" : "深圳"
    }
  ]
}

要求查询指定uid下,addrs数组中只包含is_query等于1的结果集(0的不包含)。

查询语句:

方法一:使用$unwind将addrs数组打散,获取结果集后用$match筛选符合条件的数据,最后使用$group进行聚合获取最终结果集。

db.getCollection('user').aggregate(
  [
    {  
      $unwind: "$addrs" 
    },
    { 
      $match : {
        "uid":"1000001", 
        "addrs.is_query": "1" 
      } 
    },
    { 
      $group : { 
        "_id" : "$uid", 
        "addrs": { $push: "$addrs" } 
      } 
    } 
  ]
)

Result:

{
  "_id" : "1000001",
  "addrs" : [ 
    {
      "is_query" : "1",
      "city" : "北京"
    }, 
    {
      "is_query" : "1",
      "city" : "深圳"
    }
  ]
}

方法二:使用$match过滤符合条件的根文档结果集,然后使用$project返回对应字段的同时,在addrs数组中使用$filter进行内部过滤,返回最终结果集

db.getCollection('user').aggregate(
  [
    { 
      $match : { "uid": "1000001" } 
    },
    {
      $project: {
        "uid": 1,
        "name": 1,
        "addrs": {
          $filter: {
            input: "$addrs",
            as: "item",
            cond: { $eq : ["$$item.is_query","1"] }
          }
        }
      }
    }
  ]
)

Result:

{
  "_id" : ObjectId("5bbcc0c9a74db9804e78a157"),
  "uid" : "1000001",
  "name" : "zhangsan",
  "addrs" : [ 
    {
      "is_query" : "1",
      "city" : "北京"
    }, 
    {
      "is_query" : "1",
      "city" : "深圳"
    }
  ]
}

相对于$group分组聚合返回结果集的方式,在当前查询要求下$filter显得更加优雅一些,也比较直接。当然如果包含统计操作,比如要求返回is_query等于1的数量,这时候$group就非常合适了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持来客网。