父属性上的Mongodb聚合筛选器查找
给定这样一个对象,如果父属性上的Mongodb聚合筛选器查找,mongodb,nosql,mongodb-query,aggregation-framework,nosql-aggregation,Mongodb,Nosql,Mongodb Query,Aggregation Framework,Nosql Aggregation,给定这样一个对象,如果isAnonymous字段为false,我如何使其成为lookup users info { "_id" : ObjectId(""), "user" : ObjectId(""), "title" : "This is an idea", "shortDescription" : "Pretty cool stuff", "downVoteCount" : NumberInt(0), "upVoteCount" : NumberInt(12), "voteTo
isAnonymous
字段为false
,我如何使其成为lookup users info
{
"_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
}
},
);
当然,您应该将其应用于其他两个字段。如果您甚至不想向他们显示结果,请在最后一个阶段使用筛选器$match
{
$match: { isAnonymous: { $ne: "anonymous" } }
}
可以对投影字段使用$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
}
},
);
当然,您应该将其应用于其他两个字段。如果您甚至不想向他们显示结果,请在最后一个阶段使用筛选器$match
{
$match: { isAnonymous: { $ne: "anonymous" } }
}
实际上,您可以在聚合管道中使用条件来更改投影 以下查询应以所需格式投影
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"
}
}
实际上,您可以在聚合管道中使用条件来更改投影 以下查询应以所需格式投影
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"
}
}
所以当anonymous为false时,您需要用户信息否?只是想看看问题出在哪里。顺便说一句,如果您正在寻找条件查找,则不可能。@Veeram条件查找在v3.6中可用,我对此版本没有经验,但您可以在文档中找到它:@Veeram确实可以在mongodb中进行条件查找。@RaulRueda这怎么可能?我认为应该有一个表达式来决定是否应该执行查找。你们能给我举个例子吗?3.6是基于多个条件的连接。条件$lookup与在后续阶段(如$projection或$match)中应用过滤器完全不同。请阅读我发布的链接,其中有很多关于它们如何工作以及它们具有哪些属性的示例。因此,当anonymous为false时,您需要用户信息否?只是想看看问题出在哪里。顺便说一句,如果您正在寻找条件查找,则不可能。@Veeram条件查找在v3.6中可用,我对此版本没有经验,但您可以在文档中找到它:@Veeram确实可以在mongodb中进行条件查找。@RaulRueda这怎么可能?我认为应该有一个表达式来决定是否应该执行查找。你们能给我举个例子吗?3.6是基于多个条件的连接。条件$lookup与在后续阶段(如$projection或$match)中应用过滤器完全不同。请阅读我发布的链接,其中有很多关于它们如何工作以及它们具有哪些属性的示例。