2017-04-03 171 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中的”}}。它的作用像魅力。非常感谢 ... :) –