Skip to content

Instantly share code, notes, and snippets.

@harpreetkhalsagtbit
Last active April 6, 2021 08:02
Show Gist options
  • Save harpreetkhalsagtbit/a3eeadd5c6e99650f667 to your computer and use it in GitHub Desktop.
Save harpreetkhalsagtbit/a3eeadd5c6e99650f667 to your computer and use it in GitHub Desktop.
Running multiple Mongodb/mongoose queries - dynamiclly using async.js
// 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)
});
@harpreetkhalsagtbit
Copy link
Author

output

finish {Story:
    [
        {
            title: 'A man who cooked Nintendo',
            _id: 5399d40e0e5605f406e96612,
            __v: 0
        }
    ],
Person:
    [
        {
            name: 'harpreet',
            age: 24,
            _id: 5399d45f365d88e00dad2d81,
            __v: 0
        }
    ]
}

@kpnigalye
Copy link

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