在父属性上查找Mongodb聚合过滤器

时间:2018-02-16 22:17:25

标签: mongodb nosql mongodb-query aggregation-framework nosql-aggregation

鉴于此类对象,如果isAnonymous字段为false,我怎样才能查找用户信息?

{ 
"_id" : ObjectId(""), 
"user" : ObjectId(""), 
"title" : "This is an idea", 
"shortDescription" : "Pretty cool stuff",
"downVoteCount" : NumberInt(0), 
"upVoteCount" : NumberInt(12), 
"voteTotalCount" : NumberInt(12), 
"deleted" : false, 
"ideaNumber" : NumberInt(5),
"isAnonymous" : false, 
"created" : ISODate("2018-01-19T23:37:10.949+0000")
}

这是我当前的查询:

db.ideas.aggregate(
{
    $match: { "deleted": false }
},
{
    $lookup: {
        "from" : "users",
        "localField" : "user",
        "foreignField" : "_id",
        "as" : "user"
    }
},
{
    $unwind: {
        path : "$user",
        includeArrayIndex : "arrayIndex", // optional
        preserveNullAndEmptyArrays : false // optional
    }
},
{
    $project: {
        "ideaNumber": 1,
        "title": 1,
        "isAnonymous": 1,
        "shortDescription": 1,
        "upVoteCount": 1,
        "created": 1,
        "user.email": 1,
        "user.name": 1,
        "user.createdAt": 1
    }
},
);

产生如下对象:

{ 
"_id" : ObjectId(""), 
"user" : {
    "createdAt" : ISODate("2018-01-19T21:50:02.758+0000"), 
    "email" : "blah", 
    "name" : "Foo Bar"
}, 
"title" : "This is an idea", 
"shortDescription" : "Pretty cool stuff", 
"upVoteCount" : NumberInt(12), 
"ideaNumber" : NumberInt(5), 
"isAnonymous" : false, 
"created" : ISODate("2018-01-19T23:37:10.949+0000")

}

我希望看到像这样的对象

{ 
"_id" : ObjectId(""), 
"user" : {
    "createdAt" : "anonymous", 
    "email" : "anonymous", 
    "name" : "anonymous"
}, 
"title" : "This is an idea", 
"shortDescription" : "Pretty cool stuff", 
"upVoteCount" : NumberInt(12), 
"ideaNumber" : NumberInt(5), 
"isAnonymous" : false, 
"created" : ISODate("2018-01-19T23:37:10.949+0000")
}

2 个答案:

答案 0 :(得分:1)

您可以对投影字段使用$ cond语句:

db.ideas.aggregate(
    {
        $match: { "deleted": false }
    },
    {
        $lookup: {
            "from": "users",
            "localField": "user",
            "foreignField": "_id",
            "as": "user"
        }
    },
    {
        $unwind: {
            path: "$user",
            includeArrayIndex: "arrayIndex", // optional
            preserveNullAndEmptyArrays: false // optional
        }
    },
    {
        $project: {
            "ideaNumber": 1,
            "title": 1,
            "isAnonymous": 1,
            "shortDescription": 1,
            "upVoteCount": 1,
            "created": 1,
            "user.email": {
                $cond: {
                    if: { $eq: ["$isAnonymous", true] },
                    then: "anonymous",
                    else: "$user.email"
                }
            },
            "user.name": 1,
            "user.createdAt": 1
        }
    },
);

当然,您应该将此应用于其他2个字段。如果您不想显示结果,请在最后一个阶段使用过滤器$ match。

{
    $match: { isAnonymous: { $ne: "anonymous" } }
} 

答案 1 :(得分:1)

实际上,您可以在聚合管道中使用条件来改变projection

以下查询应以所需格式投影。

db.T.aggregate([
{
    $match: { "deleted": false }
},
{
    $lookup: {
        "from" : "users",
        "localField" : "user",
        "foreignField" : "_id",
        "as" : "user"
    }
},
{
    $unwind: {
        path : "$user",
        includeArrayIndex : "arrayIndex", // optional
        preserveNullAndEmptyArrays : false // optional
    }
},
{ 
    $project:{
        "ideaNumber": 1,
        "title": 1,
        "isAnonymous": 1,
        "shortDescription": 1,
        "upVoteCount": 1,
        "created": 1,
         user : {$cond: [{$eq:["$isAnonymous", true]}, 
         {
            "createdAt" : "anonymous", 
            "email" : "anonymous", 
            "name" : "anonymous"
         }, 
         {
            "createdAt" : "$user.createdAt", 
            "email" : "$user.email", 
            "name" : "$user.name"
         }]}
    }
}]);

以上查询导致结果为:

{ 
    "_id" : ObjectId("5a876fae304c013cbf854766"), 
    "title" : "This is an idea", 
    "shortDescription" : "Pretty cool stuff", 
    "upVoteCount" : NumberInt(12), 
    "ideaNumber" : NumberInt(5), 
    "isAnonymous" : false, 
    "created" : ISODate("2018-01-19T23:37:10.949+0000"), 
    "user" : {
        "createdAt" : ISODate("2018-01-19T23:37:10.949+0000"), 
        "email" : "abc@cde.com", 
        "name" : "user A"
    }
}