Last active
April 6, 2021 08:02
-
-
Save harpreetkhalsagtbit/a3eeadd5c6e99650f667 to your computer and use it in GitHub Desktop.
Running multiple Mongodb/mongoose queries - dynamiclly using async.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// working example mongoose | |
var PersonSchema = new Schema({ | |
name : String, | |
age : Number, | |
}); | |
var StorySchema = new Schema({ | |
title : String, | |
}); | |
var Story = mongoose.model('Story', StorySchema); | |
var Person = mongoose.model('Person', PersonSchema); | |
// var story1 = new Story({ | |
// title: "A man who cooked Nintendo", | |
// }); | |
// var person1 = new Person({ | |
// name: "harpreet", | |
// age: "24" | |
// }); | |
// story1.save(function (err, result) { | |
// if(err) { | |
// console.log(err) | |
// } else { | |
// console.log(result) | |
// } | |
// }); | |
// person1.save(function (err, result) { | |
// if(err) { | |
// console.log(err) | |
// } else { | |
// console.log(result) | |
// } | |
// }); | |
// Story.find({}, function(err, result) { | |
// console.log(result); | |
// }); | |
// Person.find({}, function(err, result) { | |
// console.log(result); | |
// }); | |
/* | |
* Object to store all models | |
*/ | |
var allRefDatasSchemas = { | |
Story : mongoose.model('Story', StorySchema), | |
Person : mongoose.model('Person', PersonSchema) | |
}; | |
/* | |
* need an array to run all queries one by one in a definite order | |
*/ | |
var arr = []; | |
for(each in allRefDatasSchemas) { | |
arr.push(each); | |
} | |
/* | |
* Callback function for initiation of waterfall | |
*/ | |
var queue = [ | |
function(callback) { | |
// pass the ref array and run first query by passing starting index - 0 | |
callback(null, arr, 0) | |
} | |
]; | |
/* | |
* Object to store result of all queries | |
*/ | |
var finalResult = {}; | |
/* | |
* Generic Callback function for every dynamic query | |
*/ | |
var callbackFunc = function(prevModelData, currentIndex, callback) { | |
allRefDatasSchemas[arr[currentIndex]].find(function(err, result) { | |
if(err) { | |
console.log(err) | |
} else { | |
// console.log(result) | |
finalResult[arr[currentIndex]] = result | |
callback(null, result, currentIndex + 1) | |
} | |
}) | |
} | |
/* | |
* Add callback function for every dynamic query | |
*/ | |
for(each in allRefDatasSchemas) { | |
queue.push(callbackFunc); | |
} | |
/* | |
* Run all dynamic queries one by one using async.js waterfall method | |
*/ | |
async.waterfall(queue, function (err, result) { | |
console.log('finish', finalResult) | |
}); |
awesome. I was looking something similar to this. thanks a ton! :)
Also wanted to know if we use mongoose function 'FindOne()' for a single schema object then how can I use async with it?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output