MongoDB聚合查询$ match

时间:2017-05-17 12:44:07

标签: mongodb mongodb-query

我创建一个查询,返回农田详细信息以及与之相关的所有事件(见下文) 现在,我想过滤并仅返回status字段不等于delete的事件。我尝试使用$ match运算符,但它不起作用...是否有人可以帮助我?
以下是数据库的集合:
农田收藏

{
    "_id" : ObjectId("5909d9bc19383e2a16378271"),
    "SUID" : "IT0001",
    "id" : "A",
    "UUID" : "CAL123",
    "name" : "Pratosaiano (Maza)",
    "status" : "close"
}

活动集

...
{
    "_id" : ObjectId("5909e3387786e52c90f158da"),
    "SUID" : "IT0001",
    "farmland" : "B",
    "opening" : "00:00",
    "closing" : "00:00",
    "type" : "scheduled",
    "days" : [
        0,
        0,
        0,
        0,
        0,
        0,
        0
    ],
    "active" : true,
    "status" : "add",
    "id" : 204
},
{
    "_id" : ObjectId("590b4ec6cdddea05130fbc3b"),
    "SUID" : "IT0001",
    "farmland" : "B",
    "opening" : "00:00",
    "closing" : "01:00",
    "type" : "semiautomatic",
    "active" : true,
    "status" : "add",
    "id" : 16
}
...

查询:

db.farmlands.aggregate([
{
    $match: {  
        "SUID": "IT0001",
        "id": "B",
    }
}, 
{  
    $lookup: { 
        localField: "id", 
        from: "events", 
        foreignField: "farmland", 
        as: "events" 
    }  
},  
{  
    $project: {
        "_id": 0,  
        "SUID": 1,  
        "id": 1,  
        "name": 1, 
        "events.id": 1,  
        "events.type": 1,  
        "events.opening": 1,
        "events.closing": 1, 
        "events.days": 1, 
        "events.active": 1, 
        "events.status": 1 
    }
}]);

此查询的输出

{
    "_id" : ObjectId("5909d9bc19383e2a16378272"),
    "SUID" : "IT0001",
    "id" : "B",
    "UUID" : "CAL123",
    "name" : "Pratosaiano (Maza)",
    "status" : "close",
    "events" : [
        {
            "_id" : ObjectId("5909da8312452a289bbb62c1"),
            "SUID" : "IT0001",
            "farmland" : "B",
            "opening" : "07:00",
            "closing" : "08:00",
            "type" : "scheduled",
            "days" : [
                0,
                0,
                0,
                0,
                0,
                0,
                0
            ],
            "active" : true,
            "status" : "update",
            "id" : 237
        }, ...
    ]
}

0 个答案:

没有答案