Mongoose $ cond不工作

时间:2017-05-07 13:32:53

标签: node.js mongodb mongoose

我有以下数据

[
{
    "_id": "58fb3b7b78352e228c9e86b5",
    "item_id": "xxxxx",
    "sku": "D7C-670-A7B",
    "price": "40.73",
    "categoryname": "TV, film e videogiochi",
    "categoryid": 75708,
    "__v": 0,
    "country": "Italy",
    "specifics": {
        "NameValueList": [
            {
                "Name": "MPN",
                "Value": "Does Not Apply",
                "Source": "ItemSpecific"
            },
            {
                "Name": "EAN",
                "Value": "Non applicabile",
                "Source": "ItemSpecific"
            },
            {
                "Name": "EAN",
                "Value": "Non applicabile"
            },
            {
                "Name": "ISBN",
                "Value": "Non applicabile"
            }
        ]
    },
    "ean": "Non applicabile"
},
{
    "_id": "58fb3b7b78352e228c9e86b7",


    "price": "13.6",

    "categoryname": "Etuis, housses, coques",
    "categoryid": 20349,
    "__v": 0,
    "country": "France",

    "specifics": {
        "NameValueList": [
            {
                "Name": "Compatible Marques",
                "Value": "Pour Samsung, Apple, pour HTC, pour LG, pour Google",
                "Source": "ItemSpecific"
            },
            {
                "Name": "Compatible Modèles",
                "Value": "iPhone 5, 5S,5C,6,6S",
                "Source": "ItemSpecific"
            },
            {
                "Name": "Design/Finition",
                "Value": "Brillant et Mat",
                "Source": "ItemSpecific"
            },
            {
                "Name": "Caractéristiques",
                "Value": "Étanche, Étui Rigide",
                "Source": "ItemSpecific"
            },
            {
                "Name": "MPN",
                "Value": "Elephant",
                "Source": "ItemSpecific"
            },
            {
                "Name": "Matériau Du Produit",
                "Value": "Polycarbonate Plastique",
                "Source": "ItemSpecific"
            },
            {
                "Name": "Type :",
                "Value": "Fitted Case/Skin",
                "Source": "ItemSpecific"
            },
            {
                "Name": "Numéro de pièce fabricant",
                "Value": "Elephant",
                "Source": "ItemSpecific"
            }
        ]
    },
    "ean": ""
}]

我想获得所有具有

的项目
spicifics.NameValueList.Name = "MPN" && spicifics.NameValueList.Name = "Universal" 

我在$ aggregate查询中使用$ unwind,但它没有返回,是预期的输出。

ebayItems.aggregate([
    {$unwind:'$specifics.NameValueList'},
    {
        $group:{"_id":"$item_id"}
    },
    {$project:{"_id":1,"HasMPN":{"$cond":{
        "if":{"$eq":["$specifics.NameValueList.Name","MPN"]},
        "then":"HasMPN",
        "else":"NoMPN"
    }}}}
]).exec(function(err,item){
    if (err) {
        res.json(err)
    } else {
        res.json(item)
    }
})

上面的输出总是'NoMPN'。上述两个字段的预期输出应为“HasMPN”。

1 个答案:

答案 0 :(得分:2)

您需要在$group阶段添加要作为回复一部分的字段,以便可以在$cond表达式中使用它。

您不需要$unwind + $group$cond。您可以$match使用$all运算符和$project _id值来应用条件。

aggregate([{
        $match: {
            "specifics.NameValueList.Name": {
                $all: ["MPN", "Universal"]
            }
        }
    },
    {
        $project: {
            "_id": 1
        }
    }
])

find({"specifics.NameValueList.Name": {$all: ["MPN", "Universal"]}}, {"_id": 1})

根据OP的反馈进行更新(匹配名称和值)

find({"specifics.NameValueList":{$elemMatch:{"Name": "MPN","Value":"Universal"}}}, {"_id": 1})