[英]How to optimize the mongodb query in nodejs?
I have a code that is running in nodejs in which I have to call the MongoDB query which has some aggregation property and I have called using callbacks but the main issue is that it doesn't be working as I expected.我有一个在 nodejs 中运行的代码,我必须在其中调用具有一些聚合属性的 MongoDB 查询,并且我使用回调进行了调用,但主要问题是它没有按我预期的那样工作。 like I want to run the code async but it does not work like that I want that once the MongoDB query is finished then my another function will be called up but currently my another function called up in between the code execution, I am very upset with this approach, I need help please give a solution.就像我想异步运行代码,但它不能像那样工作,我希望一旦 MongoDB 查询完成,然后我的另一个函数将被调用,但目前我的另一个函数在代码执行之间调用,我很不高兴这种方法,我需要帮助请给出解决方案。 here is the code.这是代码。
function (done) {
var count = 0;
Wine.distinct("_id", {}).exec(function(err,winesList){
for(var x=0;x < 100;x++){
var currentWineId = winesList[x].toString();
//wineId should not be object
WTHelper.calculateAvgWinePriceAndUpdate(currentWineId, todayDate,'true', function (err, result) {
count++;
console.log("statusReport.totalCount :", count);
})
} done(null, count);
})
}, function (count) {
console.log("prateek totalCount :", count);
}
])
//Gets the avergae price of the last 4 Months of the wine
var totalCount=0;
purchase_date = getDayEndDate(purchase_date);
var threeMonthsOldDate = new Date(purchase_date);
threeMonthsOldDate.setMonth(threeMonthsOldDate.getMonth() - 3);
var startDate = getDayStartDate(threeMonthsOldDate);
var purchaseDateString = new Date(
getDayStartDate(purchase_date)
).toDateString();
console.log(
"calculateAvgWinePriceAndUpdate: starting date :" +
startDate +
" purchase_date: ",
purchase_date
);
console.log(
"calculateAvgWinePriceAndUpdate: purchaseDateString",
purchaseDateString
);
Transaction.aggregate(
[
{
$match: {
transaction_type: "Cr",
reason: {
$nin: ["Edited", "Deleted"],
},
},
},
{
$unwind: "$details",
},
{
$match: {
wine_id: wineId,
transaction_type: "Cr",
"details.price": {
$gt: 0,
},
purchase_date: {
// $gte: startDate,
$lte: purchase_date,
},
},
},
{
$group: {
_id: {
wine_id: "$wine_id",
},
total_price: {
$sum: {
$multiply: ["$details.qty", "$details.price_usd"],
},
},
total_qty: {
$sum: "$details.qty",
},
},
},
{
$sort: {
purchase_date: -1,
},
},
{
$limit: 50,
},
{
$project: {
_id: 0,
wine_id: "$_id.wine_id",
total_price: "$total_price",
total_qty: "$total_qty",
},
},
],
function (err, avgWinePriceData) {
console.log(
"calculateAvgWinePriceAndUpdate: Aggregate Result:",
avgWinePriceData
);
var purchase_date_price = 0;
var purchase_date_quantity = 0;
if (avgWinePriceData.length) {
// There are transactions
var purchase_date_price = avgWinePriceData[0].total_price;
var purchase_date_quantity = avgWinePriceData[0].total_qty;
console.log(
"calculateAvgWinePriceAndUpdate: purchase_date_price ",
purchase_date_price
);
console.log(
"calculateAvgWinePriceAndUpdate: purchase_date_quantity",
purchase_date_quantity
);
//New Avg. Wine Price.
var avgWinePrice = parseFloat(
purchase_date_price / purchase_date_quantity
);
if (isNaN(avgWinePrice)) {
avgWinePrice = 0;
}
console.log(
"calculateAvgWinePriceAndUpdate: Avg. Price for Purchase Date:",
avgWinePrice
);
console.log(
"calculateAvgWinePriceAndUpdate: Purchase Date:",
purchaseDateString
);
WinePrice.findOne(
{
wine_id: mongoose.Types.ObjectId(wineId),
price_date_string: purchaseDateString,
},
function (err, winePrice) {
//console.log('calculateAvgWinePriceAndUpdate: wine price object ',winePrice)
if (winePrice) {
winePrice.purchase_date = new Date(purchase_date);
winePrice.price = avgWinePrice;
winePrice.quantity = purchase_date_quantity;
} else {
var winePrice = new WinePrice();
winePrice.historic_price = {};
winePrice.wine_id = mongoose.Types.ObjectId(wineId);
winePrice.price = avgWinePrice;
winePrice.price_date = new Date(purchase_date);
winePrice.price_date_string = purchaseDateString;
winePrice.purchase_date = new Date(purchase_date);
winePrice.quantity = purchase_date_quantity;
}
winePrice.save(function (err) {
if (err) {
console.log(
"calculateAvgWinePriceAndUpdate: Error Saving Wine Price",
err
);
} else {
console.log(
"calculateAvgWinePriceAndUpdate: Price Updated for Purchase Date",purchaseDateString
);
if (check == 'true') {
totalCount++
mainCallback(err, totalCount);
}
}
});
});
}
}
);
};
Check the output检查输出
statusReport.totalCount : 98
calculateAvgWinePriceAndUpdate: Price Updated for Purchase Date Sat Sep 12 2020
statusReport.totalCount : 99
calculateAvgWinePriceAndUpdate: Price Updated for Purchase Date Sat Sep 12 2020
statusReport.totalCount : 100
calculateAvgWinePriceAndUpdate: Price Updated for Purchase Date Sat Sep 12 2020
Second function is missing at the bottom底部缺少第二个功能
If you queries are not inter-related the you use promise.all which will parallel execute your queries.如果您的查询不相互关联,则使用 promise.all 将并行执行您的查询。
Or they are inter-related or you you want to execute one after another then you can use the async-await approach will execute your db call synchronously in async manner.或者它们是相互关联的,或者您想一个接一个地执行,那么您可以使用 async-await 方法以异步方式同步执行您的数据库调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.