When i run this query:
db.friendRequests.aggregate([
$lookup: {
from: "users",
localField: "author",
foreignField: "_id",
pipeline: [
{
$match: {
$expr: {
friend_id: new mongoose.Types.ObjectId(userid),
},
},
},
],
as: "userdata",
}
])
It returns every entry in the collection, but theres a pipeline in it. Then why is it not working?
Can you help me? Thanks!
Playground:
https://mongoplayground.net/p/Eh2j8lU4IQl
The friend_id field is present in the friendRequests collection (source for the aggregation) not the users collection which is the target for the $lookup. Therefore that predicate should come in a $match stage that precedes the $lookup:
db.friendRequests.aggregate([
{
$match: {
"friend_id": ObjectId("636a88de3e45346191cf4257")
}
},
{
$lookup: {
from: "users",
localField: "author",
foreignField: "_id",
as: "userdata"
}
}
])
See how it works in this playground example. Note that I changed inventory to users assuming that was just a typo in the collection name in the provided playground link.
Original answer
This syntax is incorrect:
$match: {
$expr: {
friend_id: new mongoose.Types.ObjectId(userid),
},
}
You should change it to either
$match: {
friend_id: new mongoose.Types.ObjectId(userid),
}
Or
$match: {
$expr: {
$eq: [
"$friend_id", new mongoose.Types.ObjectId(userid)
]
},
}
For mongodb version under 5.0 (Thanks for the remark #user20042973):
$lookup with localField and foreignField will ignore a pipeline. Remove them and add a let key in order to enable the pipeline.
Related
I want to calculate the size of a few interrelated documents in MongoDB for a particular application User.
I am performing aggregation and in the end doing projection on the bsonSize, but when the document size exceeds the limit of 16 MB, this approach is not working.
I think there must be some better way to solve this problem, I request to the experienced developer who is viewing this question to share a better approach.
This is what my aggregation array looks like,
[
{
$match: {
user: userId
}
}, {
$lookup: {
from: 'anyvalidcollection1',
localField: 'validlocalfield1',
foreignField: 'validforeignfield1',
as: 'alias1'
}
}, $lookup: {
from: 'anyvalidcollection2',
localField: 'validlocalfield2',
foreignField: 'validforeignfield2',
as: 'alias2'
},
$lookup: {
from: 'anyvalidcollection3',
localField: 'validlocalfield3',
foreignField: 'validforeignfield3',
as: 'alias3'
},
$lookup: {
from: 'anyvalidcollection4',
localField: 'validlocalfield4',
foreignField: 'validforeignfield4',
as: 'alias4'
},
$lookup: {
from: 'anyvalidcollection5',
localField: 'validlocalfield5',
foreignField: 'validforeignfield5',
as: 'alias5'
}
}, {
$project: {
size: {
$bsonSize: '$$ROOT'
},
fileSize: '$file_data.size'
}
}, {
$unwind: {
path: '$fileSize',
includeArrayIndex: '0',
preserveNullAndEmptyArrays: false
}
}, {
$group: {
_id: 'sum',
totalSize: {
$sum: '$size'
},
totalFileSize: {
$sum: '$fileSize'
}
}
}
]
If document in the pipeline is > 16MB $bsonSize will complain of document size, even if returned documents are < 16MB.
But a simple way to solve this is to do many bson sizes, for example if you have many fields with lots of data, field1,field2,field3
You can do something like the bellow
aggregate(
[{"$set":
{"size":
{"$add":
[{"$bsonSize": {"field1": "$field1"}},
{"$bsonSize": {"field2": "$field2"}},
{"$bsonSize": {"field3": "$field3"}}]}}}])
You also have 5 lookups that looks alot, maybe you can reduce them, if you change your schema.
I'm working with this query:
customers.aggregate: [
{$lookup: {
from: "users",
localField: "_id",
foreignField: "customerId",
as: "users"
}},
{$lookup: {
from: "templates",
let: {localField: "$_id"},
pipeline: [{
$match: { $and: [{
$expr: { $eq: ["$customerId", "$$localField"]}},
{module: false}]
}}],
as: "templates"
}},
{$lookup: {
from: "publications",
localField: "_id",
foreignField: "customerId",
as: "publications"
}},
{$lookup: {
from: "documents",
let: {localField: "$_id"},
pipeline: [{
$match: { $and: [{
$expr: { $eq: ["$customerId", "$$localField"]}},
{createdAt: {$gte: {$date: "<someDate>"}}}]
}}],
as: "recentDocuments"
}}
]
In the last lookup stage I'm filtering documents with the customerId field according to the _id field and newer than <someDate> and then joining those documents to respective "customer" object.
And after this step or if possible even in this same step I would also like to add a new field to each resulting "customer" document with the counted number of all the documents (not only those that pass the time filter) from the "documents" collection with the customerId field value corresponding to the customer document's _id. And I also wish not to join those documents to the customer object as I only need a total number of documents with respective customerId. I can only use extended JSON v1 strict mode syntax.
The result would look like:
customers: [
0: {
users: [...],
templates: [...],
publications: [...],
recentDocuments: [...],
totalDocuments: <theCountedNumber>
},
1: {...},
2: {...},
...
]
Use $set and $size
db.customers.aggregate([
{
$lookup: {
from: "documents",
let: { localField: "$_id" },
pipeline: [
{
$match: {
$and: [
{ $expr: { $eq: [ "$customerId", "$$localField" ] } }
]
}
}
],
as: "recentDocuments"
}
},
{
$set: {
totalDocuments: { $size: "$recentDocuments" }
}
}
])
mongoplayground
So on Thursday I've found a proper syntax to solve my problem. It goes like:
db.customers.aggregate([
{
$lookup: {
from: "users",
localField: "_id",
foreignField: "customerId",
as: "users"
}},
{$lookup: {
from: "templates",
let: {localField: "$_id"},
pipeline: [{
$match: { $and: [{
$expr: { $eq: ["$customerId", "$$localField"]}},
{module: false}]
}}],
as: "templates"
}},
{$lookup: {
from: "publications",
localField: "_id",
foreignField: "customerId",
as: "publications"
}},
{$lookup: {
from: "documents",
let: {localField: "$_id"},
pipeline: [{
$match: { $and: [{
$expr: { $eq: ["$customerId", "$$localField"]}},
{createdAt: {$gte: {$date: "<someDate>"}}}]
}}],
as: "recentDocuments"
},
{$lookup: {
from: "documents",
let: {localField: "$_id"},
pipeline: [{
$match: {$and: [{
$expr: {$eq: ["$customerId", "$$localField"]}},
{ $count: "count" }],
as: "documentsNumber"
}}
])
This command would, in the last stage of the aggregate pipeline, go over the documents collection again, but this time would return all the documents instead of filtering by the time period, and then would swap the resulting object for every "customer" object with the array with one item being the number of all the documents. The array could be later "unwinded" with the $unwind action, but it proved to decrease the performance drastically, thus - omitted. I really hope this will help someone to solve a similar problem.
Mongo V5.03
I am using Compass for building a pipeline. And I am stuck here.
collection: hello
{
"_id" : "...",
"collection_name" : "world"
}
collection: world
{
"_id" : "..."
}
while building a pipeline with mongodb aggregation, to call another collection, we can use $lookup operator. Syntax of $lookup looks like this :
{
* from: The target collection.
* localField: The local join field.
* foreignField: The target join field.
* as: The name for the results.
* pipeline: The pipeline to run on the joined collection.
* let: Optional variables to use in the pipeline field stages.
}
For one time use, I can directly write { from : 'world' , ...}. But I want to do this instead { from : '$collection_name', ... } so that I can keep calling field value because that collection_names field is an array which I $unwind it.
Comeon tips, suggestions, solution
We probably do not have method to $lookup from dynamic collection name as of now. However, if we have already known all possibilities of the collection names, we may use multiple $lookup to perform conditional $lookup and use $setUnion to join the lookup results together.
Here is an example of knowing all 2 possibilities, world and foo; syntax in MongoDB 5.0+:
db.hello.aggregate([
{
"$lookup": {
"from": "world",
"localField": "key",
"foreignField": "_id",
"let": {
c: "$collection_name"
},
"pipeline": [
{
$match: {
$expr: {
$eq: [
"$$c",
"world"
]
}
}
}
],
"as": "worldLookup"
}
},
{
"$lookup": {
"from": "foo",
"localField": "key",
"foreignField": "_id",
"let": {
c: "$collection_name"
},
"pipeline": [
{
$match: {
$expr: {
$eq: [
"$$c",
"foo"
]
}
}
}
],
"as": "fooLookup"
}
},
{
"$project": {
collection_name: 1,
key: 1,
allLookup: {
"$setUnion": [
"$worldLookup",
"$fooLookup"
]
}
}
}
])
Here is the Mongo playground for your reference.
I'm stuck trying to combine my document results. Here is my query and data
{"_id":"5c21ab13d03013b384f0de26",
"roles":["5c21ab31d497a61195ce224c","5c21ab4ad497a6f348ce224d","5c21ab5cd497a644b6ce224e"],
"agency":"5b4ab7afd6ca361cb38d6a60","agents":["5b4ab5e897b24f1c4c8e3de3"]}
Here is the query
return db.collection('projects').aggregate([
{
$match: {
agents: ObjectId(agent)
}
},
{
$unwind: "$agents"
},
{
$lookup: {
from: "agents",
localField: "agents",
foreignField: "_id",
as: "agents"
}
},
{
$unwind: {
path: "$roles",
preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: "roles",
localField: "roles",
foreignField: "_id",
as: "roles"
}
},
{
$lookup: {
from: "agencies",
localField: "agency",
foreignField: "_id",
as: "agency"
}
}
])
As you can see, an entry in the project collection has two arrays that are unwound before a lookup on each entry is performed and then a final lookup is performed on the "agency" field.
However when I get the results from this query I am getting a document count equal to the number of roles. For example the project I am aggregating has 3 roles and 1 agent. So I am getting back an array of 3 objects, one for each role rather than a single document with the roles array containing all three roles. There is also a chance the agents array can have more than one value.
So lost...
You don't have to run $unwind before $lookup. The localField section states that:
If your localField is an array, you may want to add an $unwind stage to your pipeline. Otherwise, the equality condition between the localField and foreignField is foreignField: { $in: [ localField.elem1, localField.elem2, ... ] }
So basically if you don't run $unwind for instance on roles then instead of document per role you will get an array of roles as ObjectIds replaced by an array of objects from that second collection.
So you can try following aggregation:
db.collection('projects').aggregate([
{
$match: {
agents: ObjectId(agent)
}
},
{
$lookup: {
from: "agents",
localField: "agents",
foreignField: "_id",
as: "agents"
}
},
{
$lookup: {
from: "roles",
localField: "roles",
foreignField: "_id",
as: "roles"
}
},
{
$lookup: {
from: "agencies",
localField: "agency",
foreignField: "_id",
as: "agency"
}
}
])
The idea here is to return an array of documents of the users' followers with the information if this user is a friend of that follower or not.
So far I have:
db.getCollection('users').aggregate([
{ $match: { _id: ObjectId("588877d82523b4395039910a") } },
{ $lookup: {
from: 'users',
localField: 'followers',
foreignField: '_id',
as: 's_followers'
}
},
{
$project: {
"s_followers._id": 1,
"s_followers.isFriend": {
$in: ["s_followers.id",
{ $setIntersection: ["$friends", "$followers"] }
]}
}
}
])
But the "s_followers.id" used in the $in operator doesn't seem to retrieve the _id information from the follower, so it always returns false.
When I use a ObjectId directly, I got the result I want:
"s_followers.isFriend": {
$in: [ObjectId("588877d82523b4395039910a"),
{ $setIntersection: ["$friends", "$followers"] }
]}
But I really need this ID to be a reference to the follower _id.
Expected result would be something like:
{
"_id" : ObjectId("588877d82523b4395039910a"),
"s_followers" : [
{
"_id" : ObjectId("5888687e56be8f172844d96f"),
"isFriend" : true
},
{
"_id" : ObjectId("5888ca27d79b8b03949a6e8c"),
"isFriend" : false
}
]
}
Thanks for your help!
UPD: A different approach (maybe easier), would be to use the ID of the user that I have (the one used on $match), but I would still need to get the reference for the follower's follower array
db.getCollection('users').aggregate([
{ $match: { _id: ObjectId("588877d82523b4395039910a") } },
{ $lookup: {
from: 'users',
localField: 'followers',
foreignField: '_id',
as: 's_followers'
}
}, {
$project: {
"firstName": 1,
"s_followers._id": 1,
"s_followers.firstName": 1,
"s_followers.followers": 1,
"s_followers.isFriend": { $in: [ObjectId("588877d82523b4395039910a"), "$s_followers.followers"] }
}
}
])
UPD2: The user data structure (the part that matters)
{
followers: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
friends: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
}
FOR VERSION 3.4.0+
Ok, just got it, I'll post here the code and my understanding of it:
db.getCollection('users').aggregate([
{ $match: { _id: ObjectId("588877d82523b4395039910a") } },
{ $lookup: {
from: 'users',
localField: 'followers',
foreignField: '_id',
as: 's_followers'
}
}, {
$project: {
"firstName": 1,
"s_followers._id": 1,
"s_followers.firstName": 1,
"s_followers.followers": 1,
}
}, {
$unwind: "$s_followers"
}, {
$project: {
"firstName": "$s_followers.firstName",
"isFriend": { $in: [ObjectId("588877d82523b4395039910a"), "$s_followers.followers"] }
}
}
])
My understanding of it:
$match: match the user I'm intended to get the followers of.
$lookup: found each follower detail
$project: select the information I want to return, also get the follower list of each follower
$unwind: create a different document for each follower
With the array unwinded, I can refer to the follower's followers array, and find my object id in it :)
In my example use followers friends list to check current user is friend or not. as {$arrayElemAt:["$s_followers.friends",0]} if want to find in followers then can use "$s_followers.followers"
You can try it.
db.getCollection('user').aggregate([
{ $match: { _id: ObjectId("5714d190e6128b7e7f8d9008") } },
{$unwind:"$followers"},
{ $lookup: {
from: 'user',
localField: 'followers',
foreignField: '_id',
as: 's_followers'
}
},
{$project:{
firstName:1,
s_followers:{$arrayElemAt:["$s_followers",0]},
isFriend:{$cond:[{
$anyElementTrue:{
$map: {"input": {$arrayElemAt:["$s_followers.friends",0]},
"as": "el",
"in": { "$eq": [ "$$el", "$_id" ] }
}
}
},true,false]}
}
},
{$group:{
_id:"$_id",
s_followers:{$push:{_id:"$s_followers._id",isFriend:"$isFriend"}}
}
}
])