Node.js - MongoError: Cannot canonicalize the request: BadValue bad order array [2]

I have a database with the following document structure:

{ "_id" : ObjectId("520bea012ab230549e749cff"), "Day" : 1, "Time" : 54, "State" : "Vermont", "Airport" : "BTV", "Temperature" : 39, "Humidity" : 57, "Wind Speed" : 6, "Wind Direction" : 170, "Station Pressure" : 29.6, "Sea Level Pressure" : 150 } 

I need to find the highest “temperature” for each “state” (i.e. there are 100 documents with “State”: “Vermont”) and add the entry “month_high”: true to this document (which has the highest temperature)

Here is my code: http://pastebin.com/UbACLbSF

But when I run the program in the shell, I get the following error:

MongoError: Cannot canonicalize request: BadValue array of bad order [2]

+7
mongodb node-mongodb-native
source share
2 answers

You need to pass an object, not an array

 cursor.sort({"State": 1, "Temperature": -1}); 
+3
source share

You just need a “state" and a "temperature", right? So try projecting it before sorting. I tested the following code and it worked:

 var query = {}; var projection = { 'State':1, 'Temperature':1 }; var options = { 'sort' : [['State',1], ['Temperature',-1]] }; var cursor = db.collection('data').find(query, projection, options); 
0
source share

All Articles