I am building a mongo query in node as such:
query = {"status":{$ne:"Complete"}}, {"processId":1, "_id":0};
then I execute the query:
return this.engine.dbcollectionName.find(query).toArray(function(err, result){...
If i evaluate "conditions", the result is 'status=$ne:"Complete"'...however, my projection is missing.
Am I missing something about how to represent json in javascript variable? It is acting like I didn't include anything after the comma.
Another way to say it, in mongo CLI I want: db.collection.find( {"status":{$ne:"Complete"}}, {"processId":1, "_id":0})
whereas within node, i'm getting equivalent of (notice no projection): db.collection.find( {"status":{$ne:"Complete"}})
Any help appreciated! Thanks
Your query and projection must be separate object parameters to find:
var query = {"status":{$ne:"Complete"}};
var projection = {"processId":1, "_id":0};
this.engine.dbcollectionName.find(query, projection).toArray(function(err, result){...
New to JavaScript I see. If you want to construct an array of arguments then you need an actual array, and with the way method signatures work here you will also need the JavaScript .apply() method:
var async = require('async'),
mongodb = require('mongodb'),
MongoClient = mongodb.MongoClient;
MongoClient.connect('mongodb://localhost/test',function(err,db) {
var query = [{ "status": { "$ne": "Complete" } }, { "processId": 1, "_id": 0 } ];
db.collection('collectionname',function(err,coll) {
coll.find.apply(coll,query).toArray(function(err,docs) {
if (err) throw err;
console.log( JSON.stringify( docs, undefined, 2 ) );
});
});
});
Otherwise you are separating arguments like
coll.find.(
{ "status": { "$ne": "Complete" } },
{ "processId": 1, "_id": 0 }
).toArray(function(err,docs) {
So that each item is passed to the function individually.
.apply() lets you pass in list variables or arrays of arguments to your function. It often requires a context as the first argument, which in this case is the "collection" object that the .find() method belongs to.