具有多个$ cond的Mongodb输出字段

时间:2015-05-18 10:08:54

标签: mongodb

以下是我使用的文档示例:

{
    "_id" : ObjectId("554a1f5fe36a768b362ea5c0"),
    "store_state" : 1,
    "services" : [
        {
            "id" : "XXX",
            "state" : 1,
            "active": true
        },
        {
            "id" : "YYY",
            "state" : 1,
            "active": true
        },
        ...
    ]
}

我想用" Y"输出一个新字段。如果id是" XXX"并且活跃是真实的,并且" N"在任何其他情况下。带有" XXX"的服务元素因为每个文件上都没有id(输出" N"在这种情况下)。

以下是我的询问:

db.stores.aggregate({ 
    $match : {"store_state":1}
},
{ $project : { 
    "XXX_active": { 
        $cond: [ {
            $and:[
                {$eq:["services.$id","XXX"]},
                {$eq:["services.$active",true]}
            ]},"Y","N"
        ] }
    }
}).pretty()

但它始终为"N"字段输出"XXX_active"

我需要的预期输出是:

{ 
    "_id" : ObjectId("554a1f5de36a768b362e7e6f"), 
    "XXX_active" : "Y" 
},
{ 
    "_id" : ObjectId("554a1f5ee36a768b362e9d25"), 
    "XXX_active" : "N" 
}, 
{ 
    "_id" : ObjectId("554a1f5de36a768b362e73a5"), 
    "XXX_active" : "Y" 
} 

可能结果的其他例子:

{ 
    "_id" : ObjectId("554a1f5de36a768b362e7e6f"), 
    "XXX_active" : "Y",
    "YYY_active" : "N"
},
{ 
    "_id" : ObjectId("554a1f5ee36a768b362e9d25"), 
    "XXX_active" : "N",
    "YYY_active" : "N"
}, 
{ 
    "_id" : ObjectId("554a1f5de36a768b362e73a5"), 
    "XXX_active" : "Y",
    "YYY_active" : "Y"
} 

每个对象只有一个XXX_active且没有重复对象,但即使服务ID元素" XXX"不在场。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

首先$unwind services数组,然后使用$cond,如下所示:

db.stores.aggregate({
    "$match": {
        "store_state": 1
    }
}, {
    "$unwind": "$services"
}, {
    "$project": {
        "XXX_active": {
            "$cond": [{
                "$and": [{
                    "$eq": ["$services.id", "XXX"]
                }, {
                    "$eq": ["$services.active", true]
                }]
            }, "Y", "N"]
        }
    }
},{"$group":{"_id":"$_id","XXX_active":{"$first":"$XXX_active"}}}) //group by id

答案 1 :(得分:0)

以下聚合管道将提供所需的结果。您需要首先在services数组字段上首先应用$unwind运算符作为初始聚合管道步骤。这将从输入文档中解构services数组字段,以输出每个元素的文档。每个输出文档都使用元素值替换数组。

db.stores.aggregate([
    {
        "$match" : {"store_state": 1}
    },
    {
        "$unwind": "$services"
    },
    {
        "$project": { 
            "store_state" : 1,
            "services": 1,
            "XXX_active": { 
                "$cond": [ 
                    {
                        "$and": [
                            {"$eq":["$services.id", "XXX"]},
                            {"$eq":["$services.active",true]}
                        ]
                    },"Y","N"
                ] 
            }
        }
    },
    {
        "$match": {
            "services.id": "XXX"
        }
    }, 
    {
       "$group": {
           "_id": {
               "_id": "$_id",
               "store_state": "$store_state",
               "XXX_active": "$XXX_active"
           },
           "services": {
               "$push": "$services"
           }
       }
   },
   {
       "$project": {
           "_id": "$_id._id",
            "store_state" : "$_id.store_state",
            "services": 1,
            "XXX_active": "$_id.XXX_active"
       }
   }   
])