Reputation: 45
There is a collection of User
{ "_id" : ObjectId("5a0d45ca8af3a91847b7cf95"), "updatedAt" : ISODate("2017-11-16T09:34:14.651Z"), "createdAt" : ISODate("2017-11-16T08:01:14.119Z"), "name" : "John", "email" : "[email protected]", "groupsFavorite" : [ ObjectId("5a0d45db8af3a91847b7cf96") ], "groups" : [ ObjectId("5a0d45db8af3a91847b7cf96"), ObjectId("5a0d45e18af3a91847b7cf97") ], "__v" : 3 }
There is a collection of Groups
/* 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 }
There is a method that returns groups of a particular user.
It is necessary to add these data to the new property of favorite: true
or false
for building a table.
For example:
{ "_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" }
Upvotes: 2
Views: 120
Reputation: 13393
You can use $lookup
operator for checking the _id
and userId
fields in "User" collection. And also for determining it is in or not, you can use $eq
operator.
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"
}
}
}
}
])
Result:
/* 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"
}
Upvotes: 1