如何返回所有匹配的嵌入式数组文档,不包括数组中的所有其他字段meteor mongo

时间:2018-02-05 09:49:38

标签: mongodb meteor

我有以下收藏。 " addedDetails"是一个嵌入式数组文档。我想匹配HM项目并在kk仪表板中检索相应的值

{
        "_id" : "eJHHpB4DkBfLh9kQH",
        "dashBoardName" : "kk",
        "addedDetails" : [
                {
                        "jid" : "reZYYfWxP9Da9FdZP",
                        "job" : "job1",
                        "project" : "HM",
                        "buildStatus" : "FAILURE"
                },
                {
                        "jid" : "KvBcagCuB9DtZa9Wm",
                        "job" : "job 2",
                        "project" : "HM",
                        "buildStatus" : "SUCCESS"
                },
                {
                        "jid" : "raiTB4mQ5TmE2d2Jn",
                        "job" : "job3",
                        "project" : "CEI",
                        "buildStatus" : "FAILURE"
                },
                {
                        "jid" : "rEmuq6Shtz2vW6Pf3",
                        "job" : "job4",
                        "project" : "RI",
                        "buildStatus" : "FAILURE"
                }
        ]
}
{
        "_id" : "muzA3wjGYfk9Ye5pE",
        "dashBoardName" : "ss",
        "addedDetails" : [
                {
                        "jid" : "MkTsPB5xgkZKGShSq",
                        "job" : "job1",
                        "project" : "HM",
                        "buildStatus" : "SUCCESS"
                }
        ]
}

预期的重新调整值:

 {
        "_id" : "eJHHpB4DkBfLh9kQH",
                    "dashBoardName" : "kk",
                    "addedDetails" : [
                            {
                                    "jid" : "reZYYfWxP9Da9FdZP",
                                    "job" : "job1",
                                    "project" : "HM",
                                    "buildStatus" : "FAILURE"
                            },
                            {
                                    "jid" : "KvBcagCuB9DtZa9Wm",
                                    "job" : "job2",
                                    "project" : "HM",
                                    "buildStatus" : "SUCCESS"
                            }
        ]}
    }

我的疑问:

'listjobName': function(){
return     dashBoard.find({"dashBoardName":"kk","addedDetails.project":"HM"},{addedDetails: { $all: [{ "$elemMatch" : { project: "HM" }}]}} );
}

请有人帮我纠正查询。这里返回仪表板kk中的所有值。

2 个答案:

答案 0 :(得分:0)

SUM(Id) OVER (PARTITION BY User_ID ORDER BY Id)

答案 1 :(得分:0)

db.collection_name.aggregate( [ 
{ $unwind : "$addedDetails" },  
{ $match : 
  { 
    $and: [ { "dashBoardName" : "kk" }, { "addedDetails.project" : "HM" } ] 
  } 
},
{ $group : 
  { _id : " $_id", 
   dashBoardName : { $first : "$dashBoardName"}, 
   addedDetails : { $push : "$addedDetails" } 
  } 
}
])

输出:

{
    "_id" : "eJHHpB4DkBfLh9kQH",
    "dashBoardName" : "kk",
    "addedDetails" : [ 
        {
            "jid" : "reZYYfWxP9Da9FdZP",
            "job" : "job1",
            "project" : "HM",
            "buildStatus" : "FAILURE"
        }, 
        {
            "jid" : "KvBcagCuB9DtZa9Wm",
            "job" : "job 2",
            "project" : "HM",
            "buildStatus" : "SUCCESS"
        }
    ]
}