How can i reverse populate in mongo. I have 2 schema's
User:
var user_scheme = new mongoose.Schema({
name:String,
age:Number,
roles:{
type:mongoose.Schema.Types.ObjectId,
ref:'Role'
}
});
Role:
var role_scheme = new mongoose.Schema({
name:String,
});
Documents:
///user
{
"_id" : ObjectId("5bf9a19b01ce3e19b440aed8"),
"name" : "user1",
"age" : 22,
"roles" : ObjectId("5c0242621ab7b677e6b2e01e"),
"__v" : 0
}
///role
{
"_id" : ObjectId("5c0242621ab7b677e6b2e01e"),
"name" : "Admin"
}
Code:
User.find().populate('roles').exec(function (err, data) {
res.json(data);
})
Here i can get roles under user, But how i get users under each role.
The $lookup function by #Ashh was not working for me.
I used a more recent version :
$lookup: {
from: 'users',
localField: '_id',
foreignField: 'organization',
as: 'users'
}
You can use below $lookup aggregation
Role.aggregate([
{ "$match": { "name" : "Admin" }},
{ "$lookup": {
"from": "users",
"let": { "roleId": "$_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$roles", "$$roleId"] } } }
],
"as": "users"
}}
])
you can use mongoose-reverse-populate module.
Related
I have two collections.
Customers:
{
"_id" : ObjectId("584aac38686860d502929b8b"),
"name" : "user",
"email" : "user#gmail.com"
}
Posts:
{
"_id" : ObjectId("584aaca6686860d502929b8d"),
"title" : "Post",
"description":"description",
"user_id" : "584aac38686860d502929b8b"
}
I want to join this collection based on the user_id (from posts collection) - _id ( in customers collection).
I tried the below query:
dbo.collection('customers').aggregate([
{
$lookup:
{
from: 'posts',
localField: 'user_id',
foreignField: '_id',
as: 'posts'
}
}
])
but it's not working.
The output I am getting:
{
"_id": "584aac38686860d502929b8b",
"name": "user",
"email": "user#gmail.com",
"posts": []
}
From attached posts collection, user_id was a string but not ObjectId.
To compare, you have to convert user_id to ObjectId first.
db.customers.aggregate([
{
$lookup: {
from: "posts",
let: {
customerId: "$_id"
},
pipeline: [
{
$match: {
$expr: {
$eq: [
{
"$toObjectId": "$user_id"
},
"$$customerId"
]
}
}
}
],
as: "posts"
}
}
])
Sample Mongo Playground
Note: From your existing $lookup, you have reversed localField and foreignField.
Equality Match with a Single Join Condition
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}
I am working with node - js and mongoDB , all the queries are ok and working fine but at some point I am using a query used 4 lookups , also applied the matches based on those lookups , and applied the pagination+ sorting in the same query. But the main issue I am facing is the query taking time around 10-20 seconds to fetch the data from the database , which is really too long time period.
Here is the code snippet for the same
var products = await db.collection('catalog_products')
.aggregate([
{ $match: { categories: cat_id.toString(), status:1, verification_status:1}},
{ $lookup: { from: 'catalog_product_meta', localField: '_id', foreignField: 'product_id', as: 'meta' } }, { $unwind:"$meta" },
{ $lookup: { from: 'catalog_product_attributes', localField: '_id', foreignField: 'product_id', as: 'attributes' } }, { $unwind:"$attributes" },
{$match : {$and : [ { $or : [ { "attributes.attribute_value" : ObjectId("60f5626681cc91c83a34f6c8") }, { "attributes.attribute_value" : ObjectId("617285baaad0c6b9d269a6c5") } ] }, { $or : [ { "attributes.attribute_value" : ObjectId("61600701dc103aaf206165c3") } ] } ]}},
{ $lookup: { from: 'catalog_product_prices', localField: '_id', foreignField: 'product_id', as: 'prices' } }, { $unwind:"$prices" },
{ $match : {$and:{ 'prices.regular_price': { '$gte': 3109, '$lte': 15406 } }}},
{ $sort: sort},
{ $project: {
"_id" : 1,
"name" : "$meta.name",
"url_key" : 1,
"regular_price" : "$prices.regular_price",
"sale_price" : "$prices.sale_price",
} },
],{ "allowDiskUse" : true }).skip(36).limit(36).toArray();
I am trying to find documents in a collection, but filtered based on the value of an embedded ObjectID relation.
Mongoose schema is as follows:
const UserQualificationSchema = new Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
}
const UserSchema = new Schema(
{
fleet: {
type: [String], // Eg ["FleetA", "FleetB", "FleetC"]
required: true,
}
}
I need to find all UserQualifications where an item in the user's fleet equals a value in a filter array.
For example: Find all User Qualifications where user.fleet: {$in: ["FleetA", "FleetC"]}
I've looked at aggregations and querying inside .populate() but can't seem to get it to work.
Any ideas much appreciated.
Use aggregation query for your problem, I have created a query for you.
users.collection.json
/* 1 */
{
"_id" : ObjectId("61056c4a8cca27df3db2e4c8"),
"firstName" : "Rahul",
"lastName" : "soni",
"fleet" : [
"FleetA",
"FleetB",
"FleetC"
],
"createdAt" : ISODate("2021-07-31T15:29:14.918Z")
}
userqualifications.collection.json
/* 1 */
{
"_id" : ObjectId("61056c908cca27df3db2e4c9"),
"user" : ObjectId("61056c4a8cca27df3db2e4c8"),
"createdAt" : ISODate("2021-07-31T15:30:24.510Z")
}
aggregation query:
it will get the result only if a user has FleetA and FleetC.
if anyone is not matched then it will return 0 records
db.userqualifications.aggregate([{
"$lookup": {
"from": "users",
"localField": "user",
"foreignField": "_id",
"as": "user"
}
}, {
"$unwind": "$user"
}, {
"$match": {
"user.fleet": {
"$elemMatch": {
"$eq": "FleetA",
"$eq": "FleetC"
}
}
}
}])
Result:
/* 1 */
{
"_id" : ObjectId("61056c908cca27df3db2e4c9"),
"user" : {
"_id" : ObjectId("61056c4a8cca27df3db2e4c8"),
"firstName" : "Rahul",
"lastName" : "soni",
"fleet" : [
"FleetA",
"FleetB",
"FleetC"
],
"createdAt" : ISODate("2021-07-31T15:29:14.918Z")
},
"createdAt" : ISODate("2021-07-31T15:30:24.510Z")
}
if the goal is to get only UserQualifications then the following should be an efficient way as it can use an index on the fleet field of the User collection.
db.User.aggregate([
{
$match: {
fleet: { $in: ["FleetA", "FleetB"] }
}
},
{
$lookup: {
from: "UserQualification",
localField: "_id",
foreignField: "user",
as: "qualifications"
}
},
{
$unwind: "$qualifications"
},
{
$replaceWith: "$qualifications"
}
])
on the other hand if you start from the UserQualifications collection, you can't efficiently narrow down the records as you're filtering on something that it doesn't have the data for.
Thank you for the answer - it did achieve the results I was looking for - however I am now struggling to add a $match with $and to the aggregate to only return the qualifications where the user ID equals one inside a submitted array AND a given fleet.
I have the following aggregate:
db.UserQualifications.aggregate([{
{
$lookup: {
from: 'users',
localField: 'user',
foreignField: '_id',
as: 'user',
},
},
{
$unwind: '$user',
},
{
$match: {
$and: [
'user.fleet': {
$in: ["Fleet A", "Fleet C"], // This works on it's own
},
user: { // Also tried 'user._id'
$in: ["6033e4129070031c07fbbf29"] // Adding this returns blank array
}
]
},
}
}])
I have double checked that I am passing in the correct User ID's inside the arrays, but when I add this to the $and inside match, it only returns a blank array.
Is there another way to do this?
// Updated users collection
/* 1 */
{
"_id" : ObjectId("61056c4a8cca27df3db2e4c8"),
"firstName" : "Rahul",
"lastName" : "soni",
"fleet" : [
"Fleet A",
"Fleet B",
"Fleet C"
],
"createdAt" : ISODate("2021-07-31T15:29:14.918Z")
}
Query:
// userqualifications => this is my collection name, you can add your collection name here db.<YOUR>
db.userqualifications.aggregate([{
$lookup: {
from: 'users',
localField: 'user',
foreignField: '_id',
as: 'user',
},
},
{
$unwind: '$user',
},
{
$match: {
// $and operatory syntax [{}, {}]
$and: [{
'user.fleet': {
// "Fleet A", "Fleet C" (FleetA, FleetC) this is the my first options,
// I have changes according to your problem
$in: ["Fleet A", "Fleet C"], // This works on it's own
}
}, {
// Convert user id to ObjectId type (_bsonType)
"user._id": ObjectId("61056c4a8cca27df3db2e4c8")
}]
}
}
])
Result:
/* 1 */
{
"_id" : ObjectId("61056c908cca27df3db2e4c9"),
"user" : {
"_id" : ObjectId("61056c4a8cca27df3db2e4c8"),
"firstName" : "Rahul",
"lastName" : "soni",
"fleet" : [
"Fleet A",
"Fleet B",
"Fleet C"
],
"createdAt" : ISODate("2021-07-31T15:29:14.918Z")
},
"createdAt" : ISODate("2021-07-31T15:30:24.510Z")
}
Difference Image:
I have two collections (promotions,product) and product collection map to promotions its working fine.but I have doubt how to show particular columns in product collection.
promotional collection
{
"_id" : ObjectId("5cf7679a0b0bed2e7483b998"),
"group_name" : "Latest",
"products" :
[ObjectId("5cecc161e8c1e73478956333"),ObjectId("5cecc161e8c1e73478956334")]
}
product collection
{
"_id" : ObjectId("5cecc161e8c1e73478956333"),
"product_name" : "bourbon"
},
{
"_id" : ObjectId("5cecc161e8c1e73478956334"),
"product_name" : "bour"
}
mapping query
db.promotional.aggregate(
[
{
$lookup: {
from: "product",
localField: "products",
foreignField: "_id",
as: "products"
}
}
]
)
I tried to map product collection to promotional collection
I got Output
{
"_id" : ObjectId("5cf7679a0b0bed2e7483b998"),
"group_name" : "Latest",
"products" :
[
{
"_id" : ObjectId("5cecc161e8c1e73478956333"),
"product_name" : "bourbon"
},
{
"_id" : ObjectId("5cecc161e8c1e73478956334"),
"product_name" : "bour"
}
]
}
Expected output
{
"_id" : ObjectId("5cf7679a0b0bed2e7483b998"),
"group_name" : "Latest",
"products" :
[
{
"product_name" : "bourbon"
},
{
"product_name" : "bour"
}
]
}
You can exclude those columns using $project operator:
db.promotional.aggregate(
[
{
$lookup: {
from: "product",
localField: "products",
foreignField: "_id",
as: "products"
}
},
{
$project: {
"products._id": 0
}
}
]
)
db.promotional.aggregate([
{
$lookup: {
from: "product",
localField: "products",
foreignField: "_id",
as: "products"
}
},{$project :{products :{product_name : 1}}}
])
db.getCollection("promotional").aggregate(
// Pipeline
[
// Stage 1
{
$unwind: {
path: "$products"
}
},
// Stage 2
{
$lookup: {
from: "product",
localField: "products",
foreignField: "_id",
as: "products"
}
},
// Stage 3
{
$group: {
_id: {
_id: '$_id',
group_name: '$group_name'
},
products: {
$push: {
product_name: {
$arrayElemAt: ["$products.product_name", 0]
}
}
}
}
},
// Stage 4
{
$project: {
_id: '$_id._id',
group_name: '$_id.group_name',
products: 1
}
},
]
);
I have following post collection
{
"_id" : ObjectId("5ad5ddb15e540442a7d4213c"),
"content" : "content1",
"comments" : [ObjectId("5af2a10dc56ad8378a3fbffa")]
}
I have following comments collection
{
"_id" : ObjectId("5af2a10dc56ad8378a3fbffa"),
"likeBy" : [ObjectId("5ac8ba3582c2345af70d4658")],
"post" : ObjectId("5ad5ddb15e540442a7d4213c"),
"comment" : "comment1",
}
I made following $lookup query... Query works perfect when comments array has ids like
"comments" : [ObjectId("5af2a10dc56ad8378a3fbffa")]
... But when comments are emptied like
"comments" : []
then it return empty array... Atleast it should return the first match condition { $match: { _id: mongoose.Types.ObjectId(id) }}...
const post = await Post.aggregate([
{ $match: { _id: mongoose.Types.ObjectId(id) }},
{ $lookup: {
from: 'comments',
localField: 'comments',
foreignField: '_id',
as: 'comments'
}
},
{ $unwind: '$comments' },
{ $addFields: {
"comments.isLiked": {
$in: [
mongoose.Types.ObjectId(req.user.id),
"$comments.likeBy"
]
}
}}
{ $group: {
_id: '$_id',
content: {$first: '$content'},
comments: {$push: '$comments'}
}
}
])
So my expected result should be
{
"_id" : ObjectId("5ad5ddb15e540442a7d4213c"),
"content" : "222222222222222222222222222222222222222222",
"comments" : []
}
only preserveNullAndEmptyArrays to true default its false.
{
$unwind: {
path: '$toUserData',
preserveNullAndEmptyArrays: true,
},
},
Please refer link : https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/