有一个用户
的集合{ "_id" : ObjectId("5a0d45ca8af3a91847b7cf95"), "updatedAt" : ISODate("2017-11-16T09:34:14.651Z"), "createdAt" : ISODate("2017-11-16T08:01:14.119Z"), "name" : "John", "email" : "test1@gmail.com", "groupsFavorite" : [ ObjectId("5a0d45db8af3a91847b7cf96") ], "groups" : [ ObjectId("5a0d45db8af3a91847b7cf96"), ObjectId("5a0d45e18af3a91847b7cf97") ], "__v" : 3 }
有一组论坛
/* 1 */ { "_id" : ObjectId("5a0d45db8af3a91847b7cf96"), "updatedAt" : ISODate("2017-11-16T08:01:31.815Z"), "createdAt" : ISODate("2017-11-16T08:01:31.815Z"), "userId" : ObjectId("5a0d45ca8af3a91847b7cf95"), "title" : "New title", "slug" : "new-title-1", "description" : "Lorem", "__v" : 0 } /* 2 */ { "_id" : ObjectId("5a0d45e18af3a91847b7cf97"), "updatedAt" : ISODate("2017-11-16T08:01:37.005Z"), "createdAt" : ISODate("2017-11-16T08:01:37.005Z"), "userId" : ObjectId("5a0d45ca8af3a91847b7cf95"), "title" : "New title", "slug" : "new-title-2", "description" : "Lorem", "__v" : 0 } /* 3 */ { "_id" : ObjectId("5a0d5cb0cd59342da943d55a"), "updatedAt" : ISODate("2017-11-16T09:38:56.912Z"), "createdAt" : ISODate("2017-11-16T09:38:56.912Z"), "userId" : ObjectId("5a0d5c48cd59342da943d559"), "title" : "New title", "slug" : "new-title-3", "description" : "Lorem", "__v" : 0 }
有一种方法可以返回特定用户的组。
有必要将这些数据添加到favorite: true
或false
的新属性中以构建表格。
例如:
{ "_id" :"5a0d45db8af3a91847b7cf96", "updatedAt" : "2017-11-16T08:01:31.815Z", "createdAt" : "2017-11-16T08:01:31.815Z", "userId" : "5a0d45ca8af3a91847b7cf95", "title" : "New title", "slug" : "new-title-1", "description" : "Lorem", "favorite": "true" }, { "_id" : "5a0d45e18af3a91847b7cf97", "updatedAt" : "2017-11-16T08:01:37.005Z", "createdAt" : "2017-11-16T08:01:37.005Z", "userId" : "5a0d45ca8af3a91847b7cf95", "title" : "New title", "slug" : "new-title-2", "description" : "Lorem", "favorite": "false" }, { "_id" : "5a0d5cb0cd59342da943d55a", "updatedAt" : "2017-11-16T09:38:56.912Z", "createdAt" : "2017-11-16T09:38:56.912Z", "userId" : "5a0d5c48cd59342da943d559", "title" : "New title", "slug" : "new-title-3", "description" : "Lorem", "favorite": "false" }
答案 0 :(得分:1)
您可以使用$lookup
运算符检查“用户”集合中的_id
和userId
字段。而且,为了确定它是否存在,您可以使用$eq
运算符。
db.getCollection('Groups').aggregate([
{
$lookup:
{
from: "User",
localField: "_id",
foreignField: "groupsFavorite",
as: "FavoriteByGrp"
}
}
,{
$lookup:
{
from: "User",
localField: "userId",
foreignField: "_id",
as: "FavoriteByUsr"
}
}
,{
"$project":
{
_id:1,
updatedAt:1,
createdAt:1,
userId:1,
title:1,
slug:1,
description:1,
favorite:
{
"$cond":
{
if: { "$eq": [ "$FavoriteByGrp._id", "$FavoriteByUsr._id" ] },
then: "true",
else: "false"
}
}
}
}
])
结果:
/* 1 */
{
"_id" : ObjectId("5a0d45db8af3a91847b7cf96"),
"updatedAt" : ISODate("2017-11-16T08:01:31.815Z"),
"createdAt" : ISODate("2017-11-16T08:01:31.815Z"),
"userId" : ObjectId("5a0d45ca8af3a91847b7cf95"),
"title" : "New title",
"slug" : "new-title-1",
"description" : "Lorem",
"favorite" : "true"
}
/* 2 */
{
"_id" : ObjectId("5a0d45e18af3a91847b7cf97"),
"updatedAt" : ISODate("2017-11-16T08:01:37.005Z"),
"createdAt" : ISODate("2017-11-16T08:01:37.005Z"),
"userId" : ObjectId("5a0d45ca8af3a91847b7cf95"),
"title" : "New title",
"slug" : "new-title-2",
"description" : "Lorem",
"favorite" : "false"
}
/* 3 */
{
"_id" : ObjectId("5a0d5cb0cd59342da943d55a"),
"updatedAt" : ISODate("2017-11-16T09:38:56.912Z"),
"createdAt" : ISODate("2017-11-16T09:38:56.912Z"),
"userId" : ObjectId("5a0d5c48cd59342da943d559"),
"title" : "New title",
"slug" : "new-title-3",
"description" : "Lorem",
"favorite" : "false"
}