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

Mongodb aggregation filter lookup on parent property

给定一个这样的对象,我怎样才能让它仅在 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")
}

投影字段可以使用$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" } }
} 

事实上,您可以在聚合管道中使用条件来改变 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"
    }
}