Variables in Mongo query

If you ever want to create dynamic queries that you will then pass to a Mongo command, here’s how to do it:

var query = {};
query['key'] = value; 

// check a condition		    
if(myvar1===myval1){
   query['key1'] = true;
}

coll.find(query,{_id:0}).toArray(function(error,results){
    if(error) callback(error);
    else{
	callback(null,results);
    }
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s