amark / mongous

Simple MongoDB driver for Node.js with jQuery like syntax.
MIT License
246 stars 30 forks source link

Count ? #6

Closed joaojeronimo closed 13 years ago

joaojeronimo commented 13 years ago

Suppose this is my find query:

db('blog.users').find(function(reply){ }, {age: 25}, {})

How do I count the number of results ? From what I see db('blog.users').find(function(reply){ }, {age: 25}, {}).count() is not possible because there is not a .count() method...

Thanks in advance.

amark commented 13 years ago

reply.documents.length || reply.numberReturned

joaojeronimo commented 13 years ago

Thanks amark, I also figured that but it retrieves all the documents it finds and then counts them isn't it ? I guess that's ok for less than 100 documents, but won't it be less efficient and memory hungry specially if you want to count millions of rows ?

By the way, how do I sort ?

amark commented 13 years ago

I'd love people to jump on and help write these commands. They really aren't that hard to do - just as long as you know how MongoDB does it, it is as easy as adding a function to handle parameters and then link it to the correct javascript object.

as find() actually returns everything, it would be memory hungry. Although I'm not sure if MongoDB indexes size, either -- but at least it would be more optimized to calculate the length.