删除结果mongoose中的uncessary键

时间:2017-11-16 09:52:00

标签: mongodb mongoose

我目前正在像这样的猫鼬中得到回应

    {
        "_id" : "5a0be40836341c8ef9dc6d9d",
        "Username" : "Adkins Daugherty",
        "ContactDetail" : {
            "ApiID" : "5a0be408ed6983954888bdb5",
            "ContactName" : "Pace Roach",
        }
}

但我想这样:

    {
        "_id" : "5a0be40836341c8ef9dc6d9d",
        "Username" : "Adkins Daugherty",
        "ApiID" : "5a0be408ed6983954888bdb5",
        "ContactName" : "Pace Roach",
}

我怎么能用猫鼬做到这一点。我的收藏名称是联系人

由于

EDIT-1

db.contacts.aggregate([{ 
        $match: { Username: 'year_Adkins Daugherty' } },
        { "$unwind": "$ContactDetail" },
        { 
            "$group": { "_id": "$_id",
                        "ApiID": "$ContactDetail.ApiID" 
                    }
        }
    ])

1 个答案:

答案 0 :(得分:0)

我想我在$ project中得到了答案

return Contact.aggregate(
        [
            { $match: { Username: regex } },
            {
            $project:{
                    Username:1,
                    ApiID:"$ContactDetail.ApiID",
                    Name:"$ContactDetail.ContactName",
                    ProfileImageUrl:"$ContactDetail.ProfileImageUrl",
                    OutletName:"$ContactDetail.OutletName"

                }
            }
        ]
    ).exec()

我得到的结果是

{
        "_id": "5a0be40836341c8ef9dc6d9d",
        "Username": "year_Adkins Daugherty",
        "ApiID": "5a0be408ed6983954888bdb5",
        "Name": "Pace Roach",
        "ProfileImageUrl": "http://placehold.it/256x256",
        "OutletName": "Codehow___kjvolyi3ye"
    }