$ push但不创建推送对象的包装器

时间:2016-11-28 15:02:41

标签: node.js mongodb mongoose aggregation-framework

我有这样的小组:

{
            "$group": {
                "_id": "$conversationId",
                "conversation": {
                    "$push": {"from": {
                            "firstName": "$fromObj.firstName",
                            "lastName": "$fromObj.lastName",
                            "title": "$fromObj.title",
                            "picture": "$fromObj.picture"

                        }, "to": {
                            "firstName": "$toObj.firstName",
                            "lastName": "$toObj.lastName",
                            "title": "$toObj.title",
                            "picture": "$toObj.picture"
                        },"content": "$content"
                        }}

            }

        }

和$ project(选择):

{"$project": {
                "conversation": "$conversation"
            }
        }

它现在推送详细信息添加到单独的coversation object如何删除会话包装器?

{
 _id: "123321",
 conversation: {
   from: {
    ....   
   },
   to: {
    ...
   }
 }
}

我需要得到这样的东西:

{
 _id: "123321",
 from: {
    ....   
 },
 to: {
    ...
 }
}

所以只是删除会话父对象并将孩子留在里面。

1 个答案:

答案 0 :(得分:0)

我创建了以下文档:

db.test.find().pretty()
{
      "_id" : ObjectId("583c994c30be1871e839d52c"),
      "conversation" : {
              "from" : {
                      "cur" : "USD"
              },
              "to" : {
                      "cur" : "CAD"
              }
      }
}

运行以下查询:

db.valdmir.aggregate((
{"$unwind":"$conversation"},
{"$project":{"from":"$conversation.from","to":"$conversation.to"}}
))

我的输出:

{ 
"_id" : ObjectId("583c994c30be1871e839d52c"),
 "from" : { "cur" : "USD" }, 
"to" : { "cur" : "CAD" } 
}

希望这可以解决您的问题。