2017-05-07 139 views
0

我有以下數據

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

我使用$放鬆身心$彙總查詢,但它沒有返回的項目中,預期產出。

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'。

回答

2

您必須在$group階段添加要成爲響應部分的字段,以便可以在$cond表達式中使用該字段。

您不需要$unwind + $group$cond。您可以僅使用$match來運用$all運算符和$project,_id值來應用標準。基於OP的反饋

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

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

更新(在名稱和值匹配)

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

感謝其對我的作品,但我需要在那裏spicifics.NameValueList.Name =「 MPN「&& spicifics.NameValueList.Name =」Universal「 –

+0

歡迎您。那就是我所擁有的'{「specifics.NameValueList.Name」:{$ all:[「MPN」,「Universal」]}}'。你能提供一個例子嗎?更多https://docs.mongodb.com/manual/reference/operator/query/all/#equivalent-to-and-operation – Veeram

+0

對不起,當我運行你的查詢時,它所做的是單獨找到MPN和Universal,它應該只找到{ 「名稱」: 「MPN」, 「值」: 「通用」, 「源」: 「ItemSpecific」 } –