Answer a question

I'm trying to run this query:

db.zips.find({"state":"GA"}, {"pop":{$gt:0}}).sort({pop:1}).limit(5)

But I keep getting this error:

"errmsg" : "Unsupported projection option: pop: { $gt: 0.0 }"

When I run this query, it works perfectly:

db.zips.find({"state":"GA"}).sort({pop:1}).limit(5)

I'm trying to find the fields where "state" = "GA" and then where "pop" is greater than 0 and limit it to 5 results and sort them in ascending order.

When I put the {"pop":{$gt:0}} part as the first argument in the find function, it runs, but it ignores the fact that I only want states that equal "GA". I'm not sure how to fix this, does anyone know what's wrong?

Answers

Mongodb's find function takes two arguments, query and projection. The query that you are firing is having two objects, second being considered as projection criteria.

Your query should hold all the criteria in single object.

db.zips.find({
               "state":"GA", 
               "pop":{$gt:0}
            })
       .sort({pop:1})
       .limit(5)
Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐