I would like to share my problem with you.
So I have a 3 entities that needs to be accessed on my query:
Evaluation:
[
{
_id: 1,
questionary: 1,
subject: 1
},
{
_id: 1,
questionary: 1,
subject: 2
},
]
User
[
{
_id: 1
name: "John Doe",
photo: "photo1.jpg"
},
{
_id: 2
name: "Paul Smith",
photo: "photo2.jpg"
},
]
questionary
[
{
_id: 1,
title: "questionary 1",
"date": "2020-02-08T00:00:00.000Z"
},
{
_id: 2,
title: "questionary 2",
"date": "2020-02-09T00:00:00.000Z"
}
]
So my target is getting a data like this: A list of questionaries inside that, a list of evaluations related to a questionary, and inside a evaluation I need a user object. Like this:
[
{
"_id": "1",
"title": "questionary 1",
"evaluations": [
{
"_id": "1",
"date": "2020-04-05T18:53:46.948Z"
"user": {
_id: 1,
"name": "John Doe",
"photo": "photo1.jpg"
}
},
{
"_id": "2",
"date": "2020-04-06T18:53:46.948Z",
"user": {
_id: 1,
"name": "John Doe",
"photo": "photo1.jpg"
}
}
]
}
]
My query is:
return await Questionary.aggregate([{
$lookup: {
from: "evaluation",
localField: "_id",
foreignField: "questionary",
as: "evaluations",
}
},
{
$lookup: {
from: "user",
localField: "evaluations.user",
foreignField: "_id",
as: "user",
}
},
{
$project: {
_id: 1,
title: 1,
status: 1,
evaluations: {
_id: 1,
date: 1,
user: "$user"
}
},
},
]);
And my result is:
[
{
"_id": "1",
"title": "questionary 1",
"evaluations": [
{
"_id": "1",
"date": "2020-04-05T18:53:46.948Z"
"user": [
{
_id: 1,
"name": "John Doe",
"photo": "photo1.jpg"
},
{
_id: 2,
"name": "Paul Smith",
"photo": "photo2.jpg"
}
]
},
{
"_id": "2",
"date": "2020-04-06T18:53:46.948Z",
"user": [
{
_id: 1,
"name": "John Doe",
"photo": "photo1.jpg"
},
{
_id: 2,
"name": "Paul Smith",
"photo": "photo2.jpg"
}
]
}
]
}
]
The users of my evaluations are merging, but this is not that I want, I just want the internal information of the user inside the evaluation.
Any suggestion?
You should be able to use a nested $lookup by using uncorrelated sub-queries as described at https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#join-conditions-and-uncorrelated-sub-queries
In your case that would be:
return await Questionary.aggregate([{
$lookup: {
from: "evaluation",
let: {questId: "$_id"},
pipeline: [{
$match: {
$expr: {
$eq: ["$$questId", "$questionary"]
}
},
}, {
$lookup: {
from: "user",
localField: "subject",
foreignField: "_id",
as: "user",
}
}],
as: "evaluations"
}
}
]);
Related
My json data in mongoDB, example:
{
"_id": 1,
"name": "ABC",
"parentID": null
},
{
"_id": 2,
"name": "ABC.txt",
"parentID": 1
},
{
"_id": 3,
"name": "ABCD",
"parentID": 1
},
{
"_id": 4,
"name": "ABC 3",
"parentID": 3
},
{
"_id": 5,
"name": "ABCDFE",
"parentID": null
}
]
how can i query find only Folder1 and children of Folder1 but ignore Folder2 in mongoDB like this by nodejs expressjs:
{
"_id": 1,
"name": "ABC",
"parentID": null
},
{
"_id": 2,
"name": "ABC.txt",
"parentID": 1
},
{
"_id": 3,
"name": "ABCD",
"parentID": 1
},
{
"_id": 4,
"name": "ABC 3",
"parentID": 3
}
]
This is my code:
exports.getProofFolderById = async (req, res, next) => {
const file = await Proof.find({ _id: req.params.id})
if(!file) {
next(new Error("Folder not found!!!"))
}
res.status(200).json({
success: true,
file
})
}
Thanks for help me ❤️
You can't have two documents with the same _id, so I assume you want to find the parent by its name.
One option for the query is using $lookup:
db.collection.aggregate([
{$match: {name: "Folder1"}},
{$lookup: {
from: "collection",
localField: "_id",
foreignField: "parentID",
as: "children"
}
},
{$project: {
children: {$concatArrays: [
"$children", [{_id: "$_id", name: "$name", parentID: "$parentID"}]
]
}
}},
{$unwind: "$children"},
{$replaceRoot: {newRoot: "$children"}}
])
See how it works on the playground example
I am trying to use the $lookup method to find users for this object. But when I use it, it replaces the other objects. The data that was outputed is this
"notifications": {
"author": [
{
"username": "UnusualAbsurd",
"status": "Michael Ohare",
"createdAt": "2022-03-08T14:02:53.728Z",
"id": "1",
"avatar": "https://avatars.dicebear.com/api/avataaars/UnusualAbsurd.png"
}
]
}
But what I expected was this
"notifications": [
"author": [
{
"username": "UnusualAbsurd",
"status": "Michael Ohare",
"createdAt": "2022-03-08T14:02:53.728Z",
"id": "1",
"avatar": "https://avatars.dicebear.com/api/avataaars/UnusualAbsurd.png"
}
],
"type": "REQUEST",
"value": "1"
]
How do I make it output the expected version?
This is my code right now
const data = await this.notificatioModel.aggregate([
{
$match: { author: id },
},
{
$project: { _id: 0, __v: 0 },
},
{
$lookup: {
from: 'users',
localField: 'notifications.author',
foreignField: 'id',
as: 'notifications.author',
pipeline: [
{
$project: { _id: 0, email: 0, password: 0 },
},
],
},
},
]);
This is my notification document
This is my user document
So what I currently have is working. But what I want in addition is, that I get in the same query the matched data from Collection C with the currently logged in user.
So the point is the following: A user can have multiple CollectionA's. And users can join the CollectionA, which is specified in CollectionB.
In the aggregation I get every joined user. But what I want in addition is, that I can see the currently logged in users data from Collection B.
CollectionA.aggregate([
{
$lookup: {
from: "collectionB",
localField: "_id",
foreignField: "col_a_id",
as: "colB",
},
},
{
$match: {
"colB.user_id": mongoose.Types.ObjectId(
request.user.id
),
},
},
{
$lookup: {
from: "collectionB",
let: { id: "$_id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$$id", "$col_a_id"] },
},
},
{
$lookup: {
from: "user",
localField: "user_id",
foreignField: "_id",
as: "user",
},
},
{
$addFields: {
x: { $arrayElemAt: ["$x", 0] },
},
},
{
$project: {
param1: "$x.a",
param2: "$x.b",
},
},
],
as: "completeList",
},
},
{
$project: {
_id: 1,
param: 1,
completeList: "$completeList",
},
},
]);
Collection A:
_id: 11
param: "fff"
Collection B:
_id: 543
col_a_id: 11,
user_id: 789,
param_I_want: "only from the user who is logged in"
User:
_id: 789
param1: "trfewe",
param2: "fewfew"
So my current output is like this:
[
{
"_id": "11",
"param": "fff",
"completeList": [
{
"_id": "222",
"user_id": 789,
"col_a_id": 11,
"param1": "trfewe",
"param2": "fewfew",
},
{
"_id": "333",
"user_id": 899,
"col_a_id": 11,
"param1": "fwfer",
"param2": "gerwa",
},
]
}
...
]
And what I want is this:
[
{
"_id": "11",
"param": "fff",
"completeList": [
{
"_id": "222",
"user_id": 789,
"col_a_id": 11,
"param1": "trfewe",
"param2": "fewfew",
},
{
"_id": "333",
"user_id": 899,
"col_a_id": 11,
"param1": "fwfer",
"param2": "gerwa",
},
],
"param_I_want": "only from the user who is logged in"
}
...
]
And since I'm asking a question I add another one into it: In the aggregation I use the same lookup. The second one is in a pipeline.
Is there a way to do it in a better way? I mean if I imagine it being used by thousands of people, could there be performance issues?
If yes why and how can I improve it?
I am trying to sort by the task._id & date in desc order. I am able to sort by task._id but sortibg bydate doesnt work, I tried changing the order in aggregate still no luck. I get the response but just the order by usertasks were added in collection and not by the usertask.date
User(name, address, etc)
Task(name, icon, assignee)
UserTask(User.ObjectId, Task.ObjectId, date)
User Collection:
{
"users": [
{
"name": "Bill",
"phone": "345"
},
{
"name": "Steve",
"phone": "123"
},
{
"name": "Elon",
"phone": "567"
}
]
}
Task collection:
{
"tasks": [
{
"name": "Run 100m",
"icon": "run",
"assignee": "Elon"
},
{
"name": "Walk 1 hour",
"icon": "walk",
"assignee": "Bill"
},
{
"name": "Jog 30 minutes",
"icon": "jog",
"assignee": "Steve"
}
]
}
UserTasks:
{
"_id": "5e72fec..",
"user": "5e72fa4..",
"task": "5e72fbac..",
"date": "2020-03-03T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:37.027Z",
"updatedAt": "2020-03-19T05:10:37.027Z",
"__v": 0
},
{
"_id": "5e72fed3..",
"user": "5e72fa4e..",
"task": "5e72fbac..",
"date": "2020-03-12T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:43.296Z",
"updatedAt": "2020-03-19T05:10:43.296Z",
"__v": 0
},
{
"_id": "5e72fed6..",
"user": "5e72fa..",
"task": "5e72fb..",
"date": "2020-03-15T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:46.057Z",
"updatedAt": "2020-03-19T05:10:46.057Z",
"__v": 0
},
{
"_id": "5e72feda...",
"user": "5e72fa4..",
"task": "5e72fb..",
"date": "2020-03-07T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:50.785Z",
"updatedAt": "2020-03-19T05:10:50.785Z",
"__v": 0
}
This is the Aggregate that needs changing
UserTask.aggregate([
{
$lookup: {
from: "tasks",
localField: "task",
foreignField: "_id",
as: "matchedTask"
}
},
{
$unwind: "$matchedTask"
},
{
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "matchedUser"
}
},
{
$unwind: "$matchedUser"
},
{
$group: {
_id: "$matchedTask._id",
name: {$first: "$matchedTask.name"},
icon: {$first: "$matchedTask.icon"},
assignee: { $first: "$matchedTask.assignee" },
userdata: {
$push: {
name: "$matchedUser.name",
date: "$date"
}
}
}
},
{
$sort: { _id: 1, "userdata.date": -1 }
}
])
.exec()
.then(doc => res.status(200).json(doc))
.catch(err => res.status(400).json("Error: " + err));
The response is shown below, please note the usertask.date. it is NOT sorted
{
"_id": "5e...",
"name": "Run 100m",
"icon": "run",
"assignee": "Elon",
"userdata": [
{
"name": "Elon",
"date": "2020-03-21T20:02:38.143Z"
},
{
"name": "Bill",
"date": "2020-03-11T20:02:38.000Z"
},
{
"name": "Steve",
"date": "2020-03-19T20:02:38.000Z"
}
]
}
As you can see the it is not sorted by date - desc order. The result should be like shown below
"userdata": [
{
"name": "Elon",
"date": "2020-03-21T20:02:38.143Z"
},
{
"name": "Steve",
"date": "2020-03-19T20:02:38.000Z"
},
{
"name": "Bill",
"date": "2020-03-11T20:02:38.000Z"
}
]
$sort will use the last object which comes out from the aggregate pipe and theres no field "date" in this object:
{
$group: {
_id: "$matchedTask._id",
name: {$first: "$matchedTask.name"},
icon: {$first: "$matchedTask.icon"},
assignee: { $first: "$matchedTask.assignee" },
userdata: {
$push: {
name: "$matchedUser.name",
execDate: "$date"
}
}
}
},
your group has to returned a field named date in order to be able to sort it
{
$group: {
_id: "$matchedTask._id",
name: ....,
date: ....
}
}
{ $sort: {date: -1}}
if the value you want to sort is indide another object you must specify it on sort:
{$sort: {"userdata.date": -1}}
I fixed it, had to use sort two times, now I am able to get the result as I want
Solution provided below
UserTask.aggregate([
{
$lookup: {
from: "tasks",
localField: "task",
foreignField: "_id",
as: "matchedTask"
}
},
{
$unwind: "$matchedTask"
},
{
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "matchedUser"
}
},
{
$unwind: "$matchedUser"
},
{
$sort: { date: -1 }
},
{
$group: {
_id: "$matchedTask._id",
name: { $first: "$matchedTask.name" },
icon: { $first: "$matchedTask.icon" },
assignee: { $first: "$matchedTask.assignee" },
userdata: {
$push: {
name: "$matchedUser.name",
execDate: "$date"
}
}
}
},
{
$sort: { _id: 1 }
}
])
.exec()
.then(doc => res.status(200).json(doc))
.catch(err => res.status(400).json("Error: " + err));
I have separate collections for 'comments','products' and 'users'. The 'comments' collection contains text, product_id and user_id. When a product is fetched I want the details of product along with details of the user in the result.
I have created schema using mongoose odm. I am using aggregate function to populate the product with comments using $lookup.
Product.aggregate([
{
$match:{
_id:mongoose.Types.ObjectId(id)
}
},
{
$lookup: {
from: "comments",
localField: "_id",
foreignField: "product",
as: "comments"
}
},
{
$match:{
"comments.product":mongoose.Types.ObjectId(id)
}
},
{
$lookup: {
from: "users",
localField: "comments.user._id",
foreignField: "user",
as: "comments.user"
}
}
])
expected result is
[
{
"_id": "5cc9441feed4c258881c99cd",
"title": "Batman",
"imageUrl": "images\\1556694047310_Batman.jpg",
"price": 555,
"description": "The dark knight",
"user": "5cbca36d4acc5d538c209014",
"__v": 2,
"comments": [
{
"_id": "5cc947125c69600d58c1be05",
"date": "2019-05-01T07:12:42.229Z",
"text": "This product is very nice",
"user":{
"_id": "5cbca36d4acc5d538c209014",
"name": "Clark Kent"
}
},
{
"_id": "5cc96eb4b2834d43f8a24470",
"date": "2019-05-01T09:46:34.774Z",
"text": "Anyone can be Batman",
"user":{
"_id": "5cbca5504acc5d538c209015",
"name": "Bruce Wayne"
},
}
}
]
actual result is
[
{
"_id": "5cc9441feed4c258881c99cd",
"title": "Batman",
"imageUrl": "images\\1556694047310_Batman.jpg",
"price": 555,
"description": "The dark knight",
"user": "5cbca36d4acc5d538c209014",
"__v": 2,
"comments": {
"user": [
{
"_id": "5cbca5504acc5d538c209015",
"name": "Bruce Wayne",
"email": "batman#gotham.com",
"password": "$2a$12$L.t/nBXq/xlic25Y0a884uGxjlimuNH/tcmWLg.sNkcjJ/C40Q14m",
"contactNumber": 9999999999,
"address": "Somewhere in Gotham",
"__v": 0
},
{
"_id": "5cbca7334acc5d538c209016",
"name": "Superman",
"email": "superman#metro.com",
"password": "$2a$12$mrogzC1Am86b0DnvTzosm.qfu38Ue7RqSNcnVSoCR55PtmLddeZv.",
"contactNumber": 9999999999,
"address": "Somewhere in metropolis",
"__v": 0
},
{
"_id": "5cbca7e54acc5d538c209017",
"name": "Wonder Woman",
"email": "ww#amazon.com",
"password": "$2a$12$Vt9XZUyOTULvel5zNAsMLeoMi3HlaGJJZN7OH2XkWuoAiZtDIGaMq",
"contactNumber": 9999999999,
"address": "Somewhere in Amazon",
"__v": 0
},
{
"_id": "5cbe192934ae2944c8704a5a",
"name": "Barry Allen",
"email": "barry#flash.com",
"password": "$2a$12$k73Wp1HTMv/MhUV3BOok3OSh.nnLq3vWG1Qz9ZTO7iB7saFlxhLjW",
"contactNumber": 9999999999,
"address": "Somewhere in Central City",
"__v": 0
}
]
}
}
]
Your $lookup query of users is overwriting the comments array. Its not working as you think it'll.
You need to unwind the comments array and then run that $lookup of users and then group by the products.
Edit: I have updated the query with $group by code too. Also you can playa around with the query here:
https://mongoplayground.net/p/2EA-Glz8Hrm
Product.aggregate([
{
$match: {
_id: "5cc9441feed4c258881c99cd"
}
},
{
$lookup: {
from: "comments",
localField: "_id",
foreignField: "product",
as: "comments"
}
},
{
$unwind: "$comments"
},
{
$lookup: {
from: "users",
localField: "comments.user",
foreignField: "_id",
as: "comments.user"
}
},
{
$unwind: "$comments.user"
},
{
$group: {
_id: "$_id",
// add other fields you want to include
comments: {
$addToSet: "$comments"
}
}
},
])
As suggested by Hamza, I made following changes to my query
Product.aggregate([
{
$match: {
_id: mongoose.Types.ObjectId(id)
}
},
{
$lookup: {
from: "comments",
localField: "_id", //field from input document
foreignField: "product", // field from documents of the 'from' collection
as: "comments"
}
},
{
$unwind: "$comments"
},
{
$lookup: {
from: "users",
localField: "comments.user", //field from input document
foreignField: "_id", // field from documents of the 'from' collection
as: "comments.user"
}
},
{
$unwind: "$comments.user"
},
{
$group: {
_id: "$_id",
title: { $first: "$title" }, // $first returns the first expression of the document it encounters, ex. first title
price: { $first: "$price" },
imageUrl: { $first: "$imageUrl" },
description: { $first: "$description" },
rating: { $first: "$rating" },
comments: {
$addToSet: "$comments" // group comments and create an array
}
}
},
{
$project: {
_id: 1,
title: 1,
price: 1,
imageUrl: 1,
description: 1,
rating: 1,
comments: {
_id: 1,
text: 1,
date: 1,
user: {
_id: 1,
name: 1
}
}
}
}
])
With this I got the desired result.