Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB returns error "Can't canonicalize query" for sort function

db.getCollection('posts').find({}, {sort: {post_id: 1}})

is returning

error: {
  "$err" : "Can't canonicalize query: BadValue Unsupported projection option: sort: { post_id: 1.0 }",
  "code" : 17287
}

I'm baffled because I've run queries almost exactly like this in the past. I'm running it through Robomongo but it does not work from my Meteor application either. Can anybody explain what's going on?

like image 771
wsc Avatar asked Jul 04 '15 15:07

wsc


1 Answers

Within Meteor the syntax is nearly as you've used:

YourCollection.find({}, {sort: {post_id: 1}})

With the MongoDB shell the format is like this:

db.posts.find({}).sort({post_id: 1})

You may still get an error at this point if you have too many results, to which you can add a limit:

db.posts.find({}).sort({post_id: 1}).limit(20)
like image 122
Tarang Avatar answered Oct 18 '22 10:10

Tarang