为了账号安全,请及时绑定邮箱和手机立即绑定

如何用MongoDB过滤子文档中的数组

如何用MongoDB过滤子文档中的数组

慕勒3428872 2019-06-21 15:40:58
如何用MongoDB过滤子文档中的数组我在子文档中有这样的数组{     "_id" : ObjectId("512e28984815cbfcb21646a7"),     "list" : [         {             "a" : 1         },         {             "a" : 2         },         {             "a" : 3         },         {             "a" : 4         },         {             "a" : 5         }     ]}我能过滤一个>3的子文档吗?我的预期结果如下{     "_id" : ObjectId("512e28984815cbfcb21646a7"),     "list" : [         {             "a" : 4         },         {             "a" : 5         }     ]}我试着用$elemMatch但是返回数组中的第一个匹配元素。我的问题是:db.test.find( { _id" : ObjectId("512e28984815cbfcb21646a7") }, {      list: {          $elemMatch:              { a: { $gt:3 }              }      } } )结果返回数组中的一个元素。{ "_id" : ObjectId("512e28984815cbfcb21646a7"), "list" : [ { "a" : 4 } ] }我试着用聚合$match但不工作db.test.aggregate({$match:{_id:ObjectId("512e28984815cbfcb21646a7"), 'list.a':{$gte:5}  }})它返回数组中的所有元素{     "_id" : ObjectId("512e28984815cbfcb21646a7"),     "list" : [         {             "a" : 1         },         {             "a" : 2         },         {             "a" : 3         },         {             "a" : 4         },         {             "a" : 5         }     ]}我能过滤数组中的元素以得到预期的结果吗?
查看完整描述

3 回答

?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

使用aggregate是正确的方法,但你需要$unwind这个list数组之前,在应用$match以便筛选单个元素,然后使用$group把它重新组合起来:

db.test.aggregate(
    { $match: {_id: ObjectId("512e28984815cbfcb21646a7")}},
    { $unwind: '$list'},
    { $match: {'list.a': {$gt: 3}}},
    { $group: {_id: '$_id', list: {$push: '$list.a'}}})

产出:

{
  "result": [
    {
      "_id": ObjectId("512e28984815cbfcb21646a7"),
      "list": [
        4,
        5
      ]
    }
  ],
  "ok": 1}

MongoDB 3.2更新

从3.2版开始,您可以使用新的$filter聚合运算符以更有效地完成此操作,只需包括list控件期间所需的元素。$project:

db.test.aggregate([
    { $match: {_id: ObjectId("512e28984815cbfcb21646a7")}},
    { $project: {
        list: {$filter: {
            input: '$list',
            as: 'item',
            cond: {$gt: ['$$item.a', 3]}
        }}
    }}])


查看完整回答
反对 回复 2019-06-21
?
拉丁的传说

TA贡献1789条经验 获得超8个赞

如果需要多个匹配的子文档,上述解决方案最好。$elemMatch如果需要单个匹配的子文档作为输出,也非常有用。

db.test.find({list: {$elemMatch: {a: 1}}}, {'list.$': 1})

结果:

{
  "_id": ObjectId("..."),
  "list": [{a: 1}]}


查看完整回答
反对 回复 2019-06-21
?
守候你守候我

TA贡献1802条经验 获得超10个赞

使用$Filter聚合

根据指定的条件选择要返回的数组的子集。返回只包含与条件匹配的元素的数组。返回的元素按原来的顺序排列。

db.test.aggregate([
    {$match: {"list.a": {$gt:3}}}, // <-- match only the document which have a matching element
    {$project: {
        list: {$filter: {
            input: "$list",
            as: "list",
            cond: {$gt: ["$$list.a", 3]} //<-- filter sub-array based on condition
        }}
    }}]);


查看完整回答
反对 回复 2019-06-21
  • 3 回答
  • 0 关注
  • 1924 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信