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();
Related
I am using aggregation query to retrieve 20000 records. while retrieving it is taking much time. I will mention my query below, Please help me to improve the query performance.
Query:
[err, data] = await to(LeadsLog.aggregate([
{$lookup:{
from: "leads",
localField: "leadId",
foreignField: "_id",
as: "leadId"
}},
{$lookup:{
from: "company_contacts",
localField: "leadId.assignedTo",
foreignField: "_id",
as: "assignedTo"
}},
{
$unwind:{
path: "$leadId",
preserveNullAndEmptyArrays: true
}
},
{
$match:{"leadId.assignedTo":new mongoose.Types.ObjectId(userId),
"result":{$eq:null}}
},
{ '$facet' : {
metadata: [ { $count: "total" }, { $addFields: { page: 1 } } ],
data: [ { $skip: 0 }, { $limit: 20000 } ]
} }
] ));
LeadId:
{
"_id" : ObjectId("617a84b401c98424e00d1310"),
"status" : true,
"address" : "Howmif Trail",
"city" : "Kinawnet",
"state" : "LA",
"country" : "LA",
"pincode" : null,
"extraFormObject" : null,
"lead_name" : "Jayden",
"phone" : "(524) 387-4912",
"email" : "niligis#taptehe.cm",
"company" : ObjectId("6155c2758609663d10fff796"),
"createdBy" : ObjectId("6155c2758609663d10fff798"),
"createdAt" : ISODate("2021-10-28T11:08:40.433Z"),
"updatedAt" : ISODate("2021-10-30T04:43:49.490Z")
}
LeadLog:
{
"_id" : ObjectId("617a84bf01c98424e00daf52"),
"callLogId" : null,
"result" : null,
"assignedTo" : ObjectId("6155c2758609663d10fff798"),
"extraFormObject" : null,
"subResult" : null,
"apptDate" : null,
"nextcallDate" : ISODate("2021-10-28T11:02:50.516Z"),
"callDate" : null,
"leadId" : ObjectId("617a84b401c98424e00d1310"),
"company" : ObjectId("6155c2758609663d10fff796"),
"createdAt" : ISODate("2021-10-28T11:08:50.962Z"),
"updatedAt" : ISODate("2021-10-30T04:43:50.281Z")
}
Please help me with better solution. thank you.
There are a few simple tweaks that you can improve your existing query:
make intermediate result as small as possible; one of the common ways is pushing $match stages as early as possible
use Pipeline Coalescence Optimization as much as possible; one of the common tuples would be $lookup + $unwind combination
index the $match fields and $lookup fields
Based on the first 2 points, here is my suggested form of your query:
You can see result : {$eq: null} is pushed to first stage. The performance gain will depends on the selectivity of the clause.
the $lookup and $unwind leads are grouped together to utilize the coalescence optimization.
"leadId.assignedTo": new mongoose.Types.ObjectId(userId) is moved earlier to minimize intermediate result size
Don't forget to index the relevant $match fields and $lookup fields. From my personal experience, good usage of index will help most with the performance.
[err, data] = await to(LeadsLog.aggregate([
{
$match: {
"result": {
$eq: null
}
}
},
{
$lookup: {
from: "leads",
localField: "leadId",
foreignField: "_id",
as: "leadId"
}
},
{
$unwind: {
path: "$leadId",
preserveNullAndEmptyArrays: true
}
},
{
$match: {
"leadId.assignedTo": new mongoose.Types.ObjectId(userId)
}
},
{
$lookup: {
from: "company_contacts",
localField: "leadId.assignedTo",
foreignField: "_id",
as: "assignedTo"
}
},
{
"$facet": {
metadata: [
{
$count: "total"
},
{
$addFields: {
page: 1
}
}
],
data: [
{
$skip: 0
},
{
$limit: 20000
}
]
}
}
]));
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:
My OBJ
[{
_id:XXXXXXXXXX,
role:admin
},
{
_id:XXXXXXXXXX,
role:superUser
}]
and need results using aggregation how to solve this using aggregation
[{
name:'username'
role:'test'
}
]
I suppose you need the following
let db1 = db.get().collection(`temp1`);
let db2 = db.get().collection(`temp2`);
await db1.aggregate([
{
$lookup: {
from: "temp2",
localField: "_id", // field in the orders collection
foreignField: "_id", // field in the items collection
as: "users"
}
},
{
$replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ["$users", 0] }, "$$ROOT"] } }
},
{ $project: { users: 0 } }
]).toArray()
I have 3 mongoDB collections
I need to aggregate them with $lookup operator but I didn't find anything/**or I'm bad looking **
1st one is suppliers
{
"_id" : ObjectId("111"), //for example, in db is mongodb ids
"name" : "supplier 1",
}
{
"_id" : ObjectId("222"),
"name" : "supplier 1",
}
2nd one is clients
{
"_id" : ObjectId("333"), //for example, in db is mongodb ids
"name" : "clients 1",
}
{
"_id" : ObjectId("444"),
"name" : "clients 2",
}
and 3rd is moves
{
"_id" : ObjectId("..."), //for example, in db is mongodb ids
"moveName" : "move 1",
"agent": ObjectId("111") // this is from suppliers collection
}
{
"_id" : ObjectId("..."),
"moveName" : "move 2",
"agent": ObjectId("333") // this one is from CLIENTS collection
}
so like output I need data like this
{
"_id" : ObjectId("..."), //for example, in db is mongodb ids
"moveName" : "move 1",
**"agent": supplier 1** // this is from suppliers collection
}
{
"_id" : ObjectId("..."),
"moveName" : "move 2",
**"agent": clients 1** // this one is from CLIENTS collection
}
back end is nodejs, I`m using mongoose, how I can search in 2nd collection if noresult in 1st?
const moves = await Move.aggregate([
{ $match: query }, // here all wokrs good
{
$lookup: {
from: 'clients',
localField: 'agent',
foreignField: '_id',
as: 'agent'
}
},{ $unwind: {path: "$agent" , preserveNullAndEmptyArrays: true} },
{
$lookup: {
from: 'suppliers',
localField: 'agent',
foreignField: '_id',
as: 'agent2'
}
},
{
$project: {
operationName: 1,
agent: {$ifNull: ['$agent.name', '$agent2.name']}
}
}
])
Thank You!
As suggested by #hhharsha36, we can use $facet operator which allows to run several pipelines within a single stage.
Explanation
facet
suppliers = $lookup suppliers collection and filter only matched results
clientes = $lookup clientes collection and filter only matched results
concatArrays = We concat suppliers and clients results into a single movies array
unwind = We flatten movies array [a, b, c] -> a
b
c
replaceWith = We replace the root element [movies:a, movies:b -> a, b]
mergeObject = allows us to pick the agent name (this way we avoid 1 more stage)
db.moves.aggregate([
{
$facet: {
suppliers: [
{
$lookup: {
from: "suppliers",
localField: "agent",
foreignField: "_id",
as: "agent"
}
},
{
$match: {
agent: {
$not: {
$size: 0
}
}
}
}
],
clients: [
{
$lookup: {
from: "clients",
localField: "agent",
foreignField: "_id",
as: "agent"
}
},
{
$match: {
agent: {
$not: {
$size: 0
}
}
}
}
]
}
},
{
$project: {
movies: {
"$concatArrays": [
"$clients",
"$suppliers"
]
}
}
},
{
$unwind: "$movies"
},
{
$replaceWith: {
"$mergeObjects": [
"$movies",
{
agent: {
"$arrayElemAt": [
"$movies.agent.name",
0
]
}
}
]
}
}
])
MongoPlayground
This aggregation query gives the desired result:
db.moves.aggregate([
{
$lookup: {
from: "suppliers",
localField: "agent",
foreignField: "_id",
as: "moves_sup"
}
},
{
$unwind: { path: "$moves_sup" , preserveNullAndEmptyArrays: true }
},
{
$lookup: {
from: "clients",
localField: "agent",
foreignField: "_id",
as: "moves_client"
}
},
{
$unwind: { path: "$moves_client" , preserveNullAndEmptyArrays: true }
},
{
$addFields: {
agent: {
$cond: [ { $eq: [ { $type: "$moves_sup" }, "object" ] },
"$moves_sup.name",
{ $cond: [ { $eq: [ { $type: "$moves_client" }, "object" ] }, "$moves_client.name", "undefined" ] }
] },
moves_client: "$$REMOVE",
moves_sup: "$$REMOVE"
}
},
])
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
}
},
]
);