2017-04-03 173 views
3

嗨,我有以下的文件,我想根據多個條件從MessageList中提取消息的子文件。返回從嵌套列表基於多個匹配標準

{ 
"_id" : ObjectId("58df770371043e7087cdaadd"), 
"prospectemailid" : "[email protected]", 
"prospectid" : "1491038545032", 
"useremail" : "[email protected]", 
"threadidslist" : [ 
    { 
     "threadid" : "15b28e8e711f71b0", 
     "subject" : "sub", 
     "campaignid" : "1491460056589", 
     "messagelist" : [ 
      { 
       "emailuniqueid" : "1492376430400", 
       "messageid" : "15b28e8e711f71b0", 
       "timestamp" : "Sat Apr 01 15:16:43 IST 2017", 
       "from" : "[email protected]", 
       "body" : "Hello", 
       "labelid" : "SENT", 
       "to" : "[email protected]", 
       "messageidpayload" : "" 
      }, 
      { 
       "emailuniqueid" : "1492376430400", 
       "messageid" : "15b28ecbcbe5b32d", 
       "timestamp" : "Sat Apr 01 15:20:54 IST 2017", 
       "from" : "[email protected]", 
       "body" : "Hi", 
       "labelid" : "RECEIVED", 
       "to" : "[email protected]", 
       "messageidpayload" : "<[email protected]om>" 
      } 
     ] 
    } 
] 

}

基於 標準

預期輸出:prospectemailid = 1491038545032,USEREMAIL = XYZ @ gmail.com,threadidslist.campaignid = 1491460056589和messagelist.emailuniqueid = 1492376430400是

{ 
       "emailuniqueid" : "1492376430400", 
       "messageid" : "15b28ecbcbe5b32d", 
       "timestamp" : "Sat Apr 01 15:20:54 IST 2017", 
       "from" : "[email protected]", 
       "body" : "Hi", 
       "labelid" : "RECEIVED", 
       "to" : [email protected]", 
       "messageidpayload" : "<[email protected]om>" 
} 

謝謝..!

到目前爲止,我曾嘗試:

db.getCollection('GD').aggregate(
[ 

    { $match:{ 
     "prospectid" : "1491038545032", 
     "useremail" : "[email protected]", 
     "threadidslist.campaignid" : "1491460056589", 
     "threadidslist.messagelist.emailuniqueid" : "1492376430400" 
    } 

} 
]) 
+0

@chridam蒙戈版本:3.4,我已經包括了我已經在疑問句到目前爲止已經試過。 –

回答

2

使用$arrayElemAt$filter運營商在$project管道得到過濾嵌套數組。該$replaceRoot管道將促進過濾子文檔的頂層,替換所有其他領域。

考慮運行下面的管道,以獲得期望的結果:

db.GD.aggregate([ 
    { "$match": { 
     "prospectid" : "1491038545032", 
     "useremail" : "[email protected]", 
     "threadidslist.campaignid" : "1491460056589", 
     "threadidslist.messagelist.emailuniqueid" : "1492376430400" 
    } }, 
    { "$project": { 
     "threadidslist": { 
      "$arrayElemAt": [ 
       { 
        "$filter": { 
         "input": "$threadidslist", 
         "as": "thread", 
         "cond": { "$eq": ["$$thread.campaignid", "1491460056589"] } 
        }     
       }, 
       0 
      ] 
     } 
    } }, 
    { "$project": { 
     "messagelist": { 
      "$arrayElemAt": [ 
       { 
        "$filter": { 
         "input": "$threadidslist.messagelist", 
         "as": "msg", 
         "cond": { "$eq": ["$$msg.emailuniqueid", "1492376430400"] } 
        }     
       }, 
       0 
      ] 
     } 
    } }, 
    { "$replaceRoot": { "newRoot": "$messagelist" } } 
]) 
+0

它給了我錯誤無法識別的管線階段$ replaceroot,但刪除後{ 「$ replaceRoot」:{ 「newRoot」: 「$ MessageList中的」}}。它的作用像魅力。非常感謝 ... :) –