按字段只获取子文档数组

时间:2016-05-02 04:34:17

标签: mongodb subdocument

我有以下ServiceProvider集合。

{
    "_id" : ObjectId("5724a838588aeca6158b4567"),
    "ServiceProviderID" : 3,
    "Title" : "test1",
    "PostedMessage" : "test1",
    "TotalComments" : 0,
    "TotalShares" : 0,
    "TotalThanks" : 0,
    "AddedOn" : "2016-04-30",
    "LastModifiedOn" : "2016-04-30 12:42:32",
    "PostAttachment" : {
        "ImagePath" : ""
    },
    "PostCommentUserIDs" : [
        "3"
    ],
    "PostComments" : [
        { // Only want this array to return
            "ID" : "1462020155845421948",
            "UserID" : 3,
            "CommentMessage" : "1",
            "TotalThanks" : 0,
            "AddedOn" : "2016-04-30 12:42:35",
            "LastModifiedOn" : "2016-04-30 12:42:35",
            "CommentAttachment" : {
                "ImagePath" : ""
            }
        },
        {
            "ID" : "14620201551142120893",
            "UserID" : 3,
            "CommentMessage" : "2",
            "TotalThanks" : 0,
            "AddedOn" : "2016-04-30 12:42:35",
            "LastModifiedOn" : "2016-04-30 12:42:35",
            "CommentAttachment" : {
                "ImagePath" : ""
            }
        },
        {
            "ID" : "14620201561666853653",
            "UserID" : 3,
            "CommentMessage" : "3",
            "TotalThanks" : 0,
            "AddedOn" : "2016-04-30 12:42:36",
            "LastModifiedOn" : "2016-04-30 12:42:36",
            "CommentAttachment" : {
                "ImagePath" : ""
            }
        }
    ],
    "Subscriber" : [ ]
}

如何通过ID字段仅获取PostComments子文档数组。

我尝试了以下查询:

db.ServiceProvider.find({
    "_id" : ObjectId('5724a838588aeca6158b4567'),
    "PostComments.ID":"1462020155845421948"
},{
    "_id": 0,
    "PostComments" :{
       "$elemMatch":{
           "PostComments.ID" : "1462020155845421948"
       } 
    }
}) 

但它不起作用。谁能请帮忙。谢谢!

修改

我只想返回以下PostComments子文档数组:

{ 
     "ID" : "1462020155845421948",
     "UserID" : 3,
     "CommentMessage" : "1",
     "TotalThanks" : 0,
     "AddedOn" : "2016-04-30 12:42:35",
     "LastModifiedOn" : "2016-04-30 12:42:35",
     "CommentAttachment" : {
          "ImagePath" : ""
     }
},

1 个答案:

答案 0 :(得分:1)

由于您要在"PostComments.ID"投影中添加$elemMatch过滤器,因此您发布的查询未写入任何文档。它应该只是ID。

db.ServiceProvider.find({
    "_id" : ObjectId('5724a838588aeca6158b4567'),
    "PostComments.ID":"1462020155845421948"
},{
    "_id": 0,
    "PostComments" :{
       "$elemMatch":{
           "ID" : "1462020155845421948"
       } 
    }
}) 

您可以使用下面的聚合函数来重新整形数组元素,以便在第一级获得它(以获得预期的结果)。

db.ServiceProvider.aggregate([
    {$match : {"_id" : ObjectId('5724a838588aeca6158b4567')}},
    {$unwind : "$PostComments"},
    {$match : {"PostComments.ID":"1462020155845421948"}},
    {$project : { "_id" : 0, "ID" : "$PostComments.ID", "UserID" : "$PostComments.UserID", "CommentMessage" : "$PostComments.CommentMessage","TotalThanks" : "$PostComments.TotalThanks","CommentAttachment" : "$CommentAttachment.UserID",}}
])

此聚合函数将打印在结果下方。

{  
   "TotalThanks":0,
   "ID":"1462020155845421948",
   "UserID":3,
   "CommentMessage":"1"
}