Ferbla
Ferbla

Reputation: 61

Mongo Projection being ignored

I am very new to Mongo so I am most likely missing something very obvious, but I have not found anything on the internet to tell me what it is. I am trying to run a mongodb query from a JavaScript file but I am having trouble.

Mongo seems to be ignoring the projection part of the query, everything else is coming through fine though.

criteria = ' { "powersave_enabled" : false, "tx_rate" : { $lt : 26000 }, "rx_rate" : { $lt : 26000 }, "btyes-r" : { $ne: 0 } } ';

projection = ' {"_id":0, "hostname" : 1, "rssi" : 1, "mac" : 1, "ap_mac" : 1, "noise" : 1} ';

command = criteria + ', ' + projection;

accessPoints = db.cache_sta.find(command);

while (accessPoints.hasNext()){
  printjson( accessPoints.next() );
}

I have printed out the command and tried running it myself in mongo and it seems to be working right, but something in the JS is messed up.

Thanks in advance!

Upvotes: 1

Views: 235

Answers (1)

Tomzan
Tomzan

Reputation: 2818

Instead of concatenate the criteria and the projection pass them as objects like this:

criteria = { "powersave_enabled" : false, "tx_rate" : { $lt : 26000 }, "rx_rate" : { $lt : 26000 }, "btyes-r" : { $ne: 0 } };

projection = {"_id":0, "hostname" : 1, "rssi" : 1, "mac" : 1, "ap_mac" : 1, "noise" : 1};

accessPoints = db.cache_sta.find(criteria, projection);

while (accessPoints.hasNext()){
     printjson( accessPoints.next() );
}

Upvotes: 1

Related Questions