MongoDB数组中的聚合匹配值

时间:2019-02-23 20:50:02

标签: mongodb match aggregate

我正在与mongo的Restaurants db合作

   {
    "_id" : ObjectId("5c66fcf59e184ea712adfba6"),
    "address" : {
        "building" : "97-22",
        "coord" : [ 
            -73.8601152, 
            40.7311739
        ],
        "street" : "63 Road",
        "zipcode" : "11374"
    },
    "borough" : "Queens",
    "cuisine" : "Jewish/Kosher",
    "grades" : [ 
        {
            "date" : ISODate("2014-11-24T00:00:00.000Z"),
            "grade" : "Z",
            "score" : 20
        }, 
        {
            "date" : ISODate("2013-01-17T00:00:00.000Z"),
            "grade" : "A",
            "score" : 13
        }, 
        {
            "date" : ISODate("2012-08-02T00:00:00.000Z"),
            "grade" : "A",
            "score" : 13
        }, 
        {
            "date" : ISODate("2011-12-15T00:00:00.000Z"),
            "grade" : "B",
            "score" : 25
        }
    ],
    "name" : "Tov Kosher Kitchen",
    "restaurant_id" : "40356068"
    }

我正在尝试对匹配项进行汇总过滤。我想检查分数是否大于5

db.runCommand({
aggregate: "restaurants",
pipeline : [
{$match: {"grades": {$anyElementTrue: {"score": {$gt:5}}}}}

但我遇到此错误:

"errmsg" : "unknown operator: $anyElementTrue",

谢谢

1 个答案:

答案 0 :(得分:1)

尝试$ eleMatch

db.restaurants.aggregate([{$match: {"grades": {$elemMatch: {"score": {$gt:5}}}}}])