Sunday, 7 May 2017

Mongoose $cond not working

I have following data

[
{
    "_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": ""
}]

I wanted to get all the items which has

spicifics.NameValueList.Name = "MPN" && spicifics.NameValueList.Name = "Universal" 

I am using $unwind in $aggregate query but its not returning, the expected output.

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)
    }
})

the output for above is always 'NoMPN'. The expected Output for above two fields should be 'HasMPN'.



via DHRUV GUPTA

No comments:

Post a Comment