Mongodb $lookup on multiple fields - mongodb

I have two collections, products and properties.
I'm doing a lookup such as:
[{
$match: {
category_id: mongoose.Types.ObjectId(category_id)
}
},
{
$lookup: {
from: "properties",
localField: "category_id",
foreignField: "category_id",
as: "properties"
}
}
]
This is basically getting me all of the products that match the category_id and including the properties that match the same category_id.
I need to add an additional check, for some_id on the properties result. In otherwords, the properties should be grouped by the some_id that is returned from the products collection and matches the same key in properties. Does that make sense? Basically having the ability to have multiple local/foreign field definitions.
any idea how I could this?

Since version 3.6 we can use uncorrelated sub-queries
{
$lookup:
{
from: '<collection to join>',
let: { <var_1>: '<expression>', …, <var_n>: '<expression>' },
pipeline: [ '<pipeline to execute on the collection to join>' ],
as: <output array field>
}
}
This allows us to to have more than a single equality match on the lookup.
See also: https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

Related

In mongodb, how to add field from one collection document to another collection document based on criteria

I am having 2 collections -
Collection name is content
Collection name is surveys
I actually want to add the "type" information from the content collection to every content in every survey in the "surveys" collection.
I know in SQL we have to join these 2 tables first on _id and content_id commun columns and then add type column to suryeys table but not sure how should i do it here. I want the type value from content collection to add in every content field inside surveys collection. Please help me.
One option is:
Using $lookup to get the data from content collection
Merge the arrays using $mergeObjects and $indexOfArray. After the lookup, the content data is on someField array. This step finds the releant content from someField and put it inside the matching survey's content item, under the key type. This step also removes someField.
Format the result using $map. This step uses $map to iterate over the content array and format the data under type to contain only the wanted part.
Save it back using $merge. This step saves the result back into the collection.
db.surey.aggregate([
{$lookup: {
from: "content",
localField: "content.content_id",
foreignField: "_id",
as: "someField"
}},
{$set: {
someField: "$$REMOVE",
content: {$map: {
input: "$content",
in: {$mergeObjects: [
"$$this",
{type: {
$arrayElemAt: [
"$someField",
{$indexOfArray: ["$someField._id", "$$this.content_id"]}
]
}}
]}
}}
}},
{$set: {content: {$map: {
input: "$content",
in: {$mergeObjects: ["$$this", {type: "$$this.type.type"}]}
}}}},
{$merge: {into: "surey"}}
])
See how it works on the playground example
In mongoose (I assume you could be using mongoose, sind you added the tag) you can specify the relation in Schema definition and just just populate to add these fields to the output.
If you are not using mongoose you can use $lookup (https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/) in your aggregation pipeline.
{
from: "content",
localField: "content._id",
foreignField: "_id",
as: "someField"
}

How to get data from dynamic mongoID with aggregate in mongodb

Here is have two collections
Product
individual Product stock (collectionname is like mongoID)
1.Product collection
db.getCollection("products").insert({ _id:"60dcc2f4c8b03e500a019fda",
"name":"mobile"
})
2 dynamicID collection
db.getCollection("60dcc2f4c8b03e500a019fda").insert(
{ quantity:5,product:"60dcc2f4c8b03e500a019fda" }
)
My Query is like
db.getCollection('products').aggregate([{
$lookup: {
from: '_id',
localField: '_id',
foreignField: 'product',
as: 'stocks'
}
},
])
Getting blank Array
As of now, you can't dynamically lookup collections.
The from field cannot be an expression and must be a string literal.
There is an open feature request for this however it seems it is very unlikely to happen:
Thanks to everyone for voting and giving your input on this request. Currently, there are no plans for supporting variable `from' collection per document, that would require significant changes in the security and query optimization architectures.
Your second insert statement inserted into a collection called "60dcc2f4c8b03e500a019fda". Your $lookup refers to a non-existent collection called "_id". To make this work try this...
Insert into 2 collections...
db.getCollection("products").insert({
_id:"60dcc2f4c8b03e500a019fda",
"name":"mobile"
})
db.getCollection("inventoryStock").insert({
quantity:5,
product:"60dcc2f4c8b03e500a019fda"
})
Run Aggregation...
db.getCollection('products').aggregate([
{
$lookup:
{
from: 'inventoryStock',
localField: '_id',
foreignField: 'product',
as: 'stocks'
}
}
])
Output:
{ "_id" : "60dcc2f4c8b03e500a019fda", "name" : "mobile", "stocks" : [ { "_id" : ObjectId("60e0ffff1e165946802c244a"), "quantity" : 5, "product" : "60dcc2f4c8b03e500a019fda" } ] }

MongoDB $lookup to replace only the ID in an array of objects

I have the following example JSON-object saved in my mongodb-collection named "Profile"
{
name: "Test",
relations: [
{
personid: <MongoDB-ID>,
type: "Friend",
duration: 5
},
{
personid: <MongoDB-ID>,
type: "Family",
duration: 9
},
]
}
I've used the mongoose-Aggregate function because i need to add artificial fields based on caluclation in the documents saved. At the end of my aggregation i use the $lookup-function to replace the property "personID" in the objects inside of the "relations"-array.
{
$lookup:
{
from: PersonModel.collection.name,
localField: 'relations.personid',
foreignField: '_id',
as: "relations.personid"
}
}
Because i want each "person" in the array of objects to replaced within the populated from the specific person-document.
This does not work as expected.
I also tried to call ".populate()" on the result returned by the aggregate function which also not worked.
Setting localField to relations.personid is not supported. localField either needs to point to an array where each member is used for the join or be a plain value. The usual way of getting around this is to $unwind first, perform the lookup, and then $group back if needed.

MongoDB create a filtered collection with 2 collections data

I have a mongo database that consist of huge github data (users, issues, repos, etc).
I want to create small collections from this big data.
I sorted "users" collection according to "followers" count of users.
Then I got the first 1000 users from this query.
db.getCollection("users").find({}).sort({followers:-1}).limit(1000).forEach(function(doc){
db.usersnew.insert(doc);});
There is another collection called "repos" that consists of info about users' repository. (user key field :"owner.id" )
I want to create a new filtered repos collection which consists only users who present in usersnew collection.
I tried to use $look_up but it works like join.
db.getCollection('reposnew').aggregate([{
$lookup:
{
from: "users",
localField: "owner:id",
foreignField : "id",
as: "filteredRepo"
}
}])
It creates users collection + repos in a one collection.
I want only filtered repos collection with specific users' data.
You're on the right track, you just need to add an $out stage.
db.getCollection('reposnew').aggregate([
{
$lookup:
{
from: "users",
localField: "owner.id",
foreignField : "id",
as: "filteredRepo"
}
},
{
$match: {
"filteredRepo.0": {$exists: true}
}
},
{
$project: {
filteredRepo: 0
}
},
{
$out: "newCollectionName"
}
])

MongoDB query, filter using cursor

I have two collections, one that has _id and UserId, and another that has UserId (same unique identifier) and "other data".
I want to filter the latter collection based on a list of _ids from the former collection.
Can someone provide an example query for this scenario?
The only way to 'join' collections in MongoDB is a $lookup aggregation stage (available in version 3.2).
firstCollection.aggregate([
{ $match: { _id: {$in: [1,2,3] }}}, // filter by _ids
{
$lookup:
{
from: "secondCollection",
localField: "UserId",
foreignField: "UserId",
as: "data"
}
}
])
That will add 'data' field to the documents from the first collection which will contain all related documents from second collection. If relation is not 1:1, you can add $unwind stage to flatten results:
{$unwind: "$data"}