【问题标题】:Return sub-document from nested list based on multiple match criteria根据多个匹配条件从嵌套列表中返回子文档
【发布时间】:2017-04-03 08:36:00
【问题描述】:

您好,我有以下文档,我想根据多个条件从消息列表中提取消息。

{
"_id" : ObjectId("58df770371043e7087cdaadd"),
"prospectemailid" : "abc@gmail.com",
"prospectid" : "1491038545032",
"useremail" : "xyz@gmail.com",
"threadidslist" : [ 
    {
        "threadid" : "15b28e8e711f71b0",
        "subject" : "sub",
        "campaignid" : "1491460056589",
        "messagelist" : [ 
            {
                "emailuniqueid" : "1492376430400",
                "messageid" : "15b28e8e711f71b0",
                "timestamp" : "Sat Apr 01 15:16:43 IST 2017",
                "from" : "utsavanand.work@gmail.com",
                "body" : "Hello",
                "labelid" : "SENT",
                "to" : "anuragkv10@gmail.com",
                "messageidpayload" : ""
            }, 
            {
                "emailuniqueid" : "1492376430400",
                "messageid" : "15b28ecbcbe5b32d",
                "timestamp" : "Sat Apr 01 15:20:54 IST 2017",
                "from" : "anuragkv10@gmail.com",
                "body" : "Hi",
                "labelid" : "RECEIVED",
                "to" : "utsavanand.work@gmail.com",
                "messageidpayload" : "<CAL_CU77Rc27peuGde=WTC7waW3gfvS2Wr_t2A+7KBjjxsKW8Sw@mail.gmail.com>"
            }
        ]
    }
]

}

预期输出基于 标准:prospecemailid=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" : "sad@gmail.com",
                "body" : "Hi",
                "labelid" : "RECEIVED",
                "to" : asd@gmail.com",
                "messageidpayload" : "<CAL_CU77Rc27peuGde=WTC7waW3gfvS2Wr_t2A+7KBjjxsKW8Sw@mail.gmail.com>"
 }

谢谢..!

到目前为止我已经尝试过:

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

   { $match:{
        "prospectid" : "1491038545032",
        "useremail" : "xyz@gmail.com",
        "threadidslist.campaignid" : "1491460056589",
        "threadidslist.messagelist.emailuniqueid" : "1492376430400"
    }

}   
])

【问题讨论】:

  • @chridam Mongo 版本:3.4,我已经在问题中包含了我迄今为止尝试过的内容。

标签: mongodb mongodb-query aggregation-framework


【解决方案1】:

$project 管道中使用 $arrayElemAt$filter 运算符来获取过滤后的嵌套数组。 $replaceRoot 管道会将过滤后的子文档提升到顶层并替换所有其他字段。

考虑运行以下管道以获得所需的结果:

db.GD.aggregate([
    { "$match": {
        "prospectid" : "1491038545032",
        "useremail" : "xyz@gmail.com",
        "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" } }
])

【讨论】:

  • 它给了我错误无法识别的管道阶段 $replaceroot,但在删除 { "$replaceRoot": { "newRoot": "$messagelist" } } 之后。它就像魅力一样。非常感谢... :)
猜你喜欢
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
  • 2019-08-21
  • 2023-03-26
  • 2018-05-27
  • 2021-05-01
相关资源
最近更新 更多