如何获取与user_list ID相关的资产

时间:2018-07-17 10:46:32

标签: node.js mongodb mongoose mongodb-query

我有以下结果,我想根据我的要求获取记录。 我有用户id,我只希望仅匹配user_list id的那些资产记录,但是它向我显示了所有与广告系列相关的资产记录,我只希望基于user_list[ID]的记录,请帮帮我,谢谢< / p>

示例:假设我有5a226aa9c5941e2f845667c3 userid我只想要与此ID相关的资产

export function getCampaigns(req, res) {  
  return Campaign.find({"assets.users_list": req.params.riderid ,status: 'Executed'}).exec()
    .then(handleEntityNotFound(res))
    .then(respondWithResult(res))
    .catch(handleError(res));
}

{
    "_id" : ObjectId("5b49d08db8695590d4ea7204"),
    "company_id" : ObjectId("59e07c60bdd6d7a454a89304"),
    "brand_id" : ObjectId("59e090a8bdd6d7a454a89305"),
    "campaign_name" : "testing campaign 1",  
    "no_of_days" : "7",
    "status" : "Executed",
    "updated_date" : ISODate("2018-07-14T10:29:33.164Z"),
    "created_date" : ISODate("2018-07-14T10:29:33.164Z"),
    "active" : true,   
    "assets" : [ 
        {
            "_id" : ObjectId("5b49cef1b8695590d4ea7203"),         
            "brand_id" : ObjectId("59e090a8bdd6d7a454a89305"),
            "medium" : "Billboard", 
            "quantity" : "1",
            "image" : "1531563759445.JPG",
            "__v" : 0,         
            "assets_planned_end_date" : ISODate("2018-07-14T10:30:13.000Z"),
            "assets_planned_start_date" : ISODate("2018-07-14T10:30:13.000Z"),
            "users_list" : [ 
                ObjectId("5a226aa9c5941e2f845667c3")
            ],
            "outdoor_tracking" : []
        }, 
        {
            "_id" : ObjectId("5b49cef1b8695590d4ea7203"),         
            "brand_id" : ObjectId("59e090a8bdd6d7a454a89305"),
            "medium" : "Billboard 2",
            "quantity" : "5",
            "image" : "1531562115.JPG",
            "__v" : 0,         
            "assets_planned_end_date" : ISODate("2018-07-14T10:30:13.000Z"),
            "assets_planned_start_date" : ISODate("2018-07-14T10:30:13.000Z"),
            "users_list" : [ 
                ObjectId("5a1d0c63ea1c673058e88d2c")
            ],
            "outdoor_tracking" : []
        },
    ],
    "__v" : 1
}

我希望广告系列记录和资产仅记录与user_list ID相关的资产

enter image description here

1 个答案:

答案 0 :(得分:0)

您仅在查询中定义条件,而不是投影。您只需要投影所需的数组元素。不知道确切的猫鼬语法,但翻译此查询:

db['01'].find(
{"assets.users_list":ObjectId("5a226aa9c5941e2f845667c3")}, // <= query part
{assets:{$elemMatch:{users_list:ObjectId("5a226aa9c5941e2f845667c3")}},
  campaign_name:1,company_id:1}  //<= projection part
)

结果如下:

    {
    "_id" : ObjectId("5b49d08db8695590d4ea7204"), 
    "company_id" : ObjectId("59e07c60bdd6d7a454a89304"), 
    "campaign_name" : "testing campaign 1", 
    "assets" : [
        {
            "_id" : ObjectId("5b49cef1b8695590d4ea7203"), 
            "brand_id" : ObjectId("59e090a8bdd6d7a454a89305"), 
            "medium" : "Billboard", 
            "quantity" : "1", 
            "image" : "1531563759445.JPG", 
            "__v" : 0, 
            "assets_planned_end_date" : ISODate("2018-07-14T12:30:13.000+0200"), 
            "assets_planned_start_date" : ISODate("2018-07-14T12:30:13.000+0200"), 
            "users_list" : [
                ObjectId("5a226aa9c5941e2f845667c3")
            ], 
            "outdoor_tracking" : [

            ]
        }
    ]
}