Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making mongoose.js queries run synchronously

I have two mongoose collections. The first stores a list of places, the second is visits to the places. My node code goes through and attempts to get the list of visits to each place and build a string that I output as JSON. The first query completes before the second ever starts - is there a way to make them run synchronously?

like image 802
AlexKinsella Avatar asked Jun 19 '13 00:06

AlexKinsella


People also ask

Can Mongoose queries be chained?

Mongoose lets you structure queries using chaining or, equivalently, using POJOs in a single function call. Model. find() returns a query, which has a separate find() method that lets you attach additional filters.

How do Mongoose relationships work?

Mongoose Relationships Tutorial Summary To model relationships between connected data, you can reference a document or embed it in another document as a sub document. Referencing a document does not create a “real” relationship between these two documents as does with a relational database.

Can I use $in in Mongoose?

The simple and easy way is to use the $in operator. The $in operator takes an array as its value.

Can I use MongoDB and Mongoose at the same time?

yes you should, its a good practice. Mongoose requires a connection to a MongoDB database. You can use require() and connect to a locally hosted database with mongoose.


1 Answers

If you are using node.js then u should use https://github.com/caolan/async

when you have to fetch data from multiple collections you have to chain your queries multiple times.

It will make your code complex and difficult to read and no modularity. Use async to create modularity using mongodb and node.js

Example Code from my project :

var async = require('async');  var createGlobalGroup = function(socket, data) {     async.waterfall(     [     /**      * this function is required to pass data recieved from client      * @param  {Function} callback To pass data recieved from client      */      function(callback) {         callback(null, socket, data);     },     /**      * Step 1: Verify User      */     verifyUser,     /**      * Step 2: Check User Access Rights And Roles      */     checkUserAccessRightsAndRoles,     /**      * Step 3: Create Project      */     createNewGlobalGroup], function(err, result) {         /**          * function to be called when all functions in async array has been called          */         console.log('project created ....')     }); } verifyUser = function(socket, data, callback) { //do your query     /**      * call next function in series      * provide sufficient input to next function      */     callback(null, socket, data, {         "isValidUser": true,     }); }  checkUserAccessRightsAndRoles = function(socket, data, asyncObj, callback) {     //do your query     if(condition) {         callback(null, socket, data, {             roles: result,             "isValidUser": asyncObj.isValidUser,             "userId": asyncObj.userId,         });     } else {     //no call back     } }  var createNewGlobalGroup = function(socket, data, asyncObj, callback) { //wanna stop then no callback } 
like image 68
Harpreet Singh Avatar answered Sep 18 '22 14:09

Harpreet Singh