如何在mongodb中聚合数组数据

时间:2017-12-07 09:05:16

标签: arrays mongodb aggregate

我有一些问题要使聚合查询在MongoDB中正常工作。

我有这个json架构:

{
    "_id" : 11,
    "name": "A green door",
    "price": 12.50,
    "tags": ["home", "green"],
    "pools" : [ 
        {
            "score" : 2,
            "language" : "en",
            "option" : "Have problems to close!",
            "time" : "2017-06-06"
        }, 
        {
            "score" : 1,
            "language" : "en",
            "option" : "I can't close this door!",
            "time" : "2017-06-06"
        }, 
        {
            "score" : 4,
            "language" : "en",
            "option" : "After some time, I'm able to install this door. Works fine!",
            "time" : "2017-06-06"
        }, 
        {
            "score" : 5,
            "language" : "fr",
            "option" : "Très difficile à installer, mais ça marche quand même.",
            "time" : "2017-06-06"
        }, 
        {
            "score" : 1,
            "language" : "es",
            "option" : "Perdí mucho tiempo para instalar.",
            "time" : "2017-06-06"
        }
    ]
}

我想只过滤使用英语(“en”)并且得分等于或大于3的注册表。所以,我试试这个:

db.test.aggregate(
    { $match: { "pools.language": "en" }},
    { $match: {"pools.score": { $gte : 3 }}},
    { $unwind: '$pools'})

但是,我总是得到其他语言的结果和不连贯的分数。

那么,如何正确地写这个呢?

3 个答案:

答案 0 :(得分:0)

首先应该展开游泳池,然后运行比赛:

db.getCollection('notification_collection').aggregate(
    { $unwind: '$pools'},
    { $match: { "pools.language": "en" }},
    { $match: {"pools.score": { $gte : 3 }}}
)

答案 1 :(得分:0)

来自https://docs.mongodb.com/v3.4/reference/operator/aggregation/

  

在db.collection.aggregate方法中,管道阶段出现在数组中。文档按顺序通过阶段

所以你应该首先放松,多个匹配可以使用$and运算符:

db.test.aggregate(
  { $unwind: '$pools' },
  { $match:
    { $and: [{ "pools.language": "en" }, { "pools.score": { $gte: 3 } }]}
  }
)

答案 2 :(得分:0)

db.test.aggregate([{ 
   $addFields:{
  pools:{
    $filter:{
     input:"$pools",
     as:"p",
     cond:{$and:[{$eq:["$$p.language","en"]},{$gte:["$$p.score",3]}]}
    }}
  }

}])

for< mongodb 3.4

db.test.aggregate([{ 
   $project:{
"_id" : 1,
"name":1,
"price":1,
"tags":1,
  pools:{
    $filter:{
     input:"$pools",
     as:"p",
     cond:{$and:[{$eq:["$$p.language","en"]},{$gte:["$$p.score",3]}]}
    }}
  }

}])