webmagnets
webmagnets

Reputation: 2296

Why is my Meteor app's greater than ($gt) count different than mongdb shell's?

Here is my input and output in the mongodb shell:

meteor:PRIMARY> db.users.count({"profile.score": {$gt: 50}})
2

Here is my code:

var allUsers = Meteor.users.find();
var newCurrentRank = allUsers.count({"profile.score": {$gt: 50}});
console.log("newCurrentRank", newCurrentRank);

Here is my console:

I20161004-11:43:42.910(0)? newCurrentRank 12

Upvotes: 1

Views: 127

Answers (1)

chridam
chridam

Reputation: 103345

It's because the count() method in Meteor returns the number of documents from a cursor that match a query. In itself passing a query as argument will not affect the count. Hence in your example above it was returning all the 12 documents despite passing the query object as argument, the count is based on the find() cursor which is returning all the documents in the collection since it was called without any query.

What you need is to call the find() cursor with the query object and then call the count() method:

var newCurrentRank = Meteor.users.find({ "profile.score": { "$gt": 50 } }).count();
console.log("newCurrentRank", newCurrentRank);

Upvotes: 2

Related Questions