2

I would like to count distinct values in a MongoDB which are between certain dates.

Something like the following in SQL:

select distinct(count(iid)) 
from collection
Where date > 2015-04-03 and date < 2015-04-04

I have been running successfully the following query on the MongoDB, however, this returns the total count (not the count of distinct values):

db.m_2015_04.find(
{ "_timestamp" : { "$gte" : ISODate("2015-04-03T00:00:000Z"), "$lt" : ISODate("2015-04-03T23:59:000Z") }})
.count()
1
  • unfortunately ,this result with an error. Commented Apr 6, 2015 at 13:56

1 Answer 1

3

what about following ?

db.m_2015_04.distinct('iid', { "_timestamp" : { "$gte" : ISODate("2015-04-03T00:00:000Z"), "$lt" : ISODate("2015-04-03T23:59:000Z") }}).length
Sign up to request clarification or add additional context in comments.

1 Comment

just be aware that distinct will not work if the distinct values exceed 16 MBytes. in this case you got to use aggregation framework or a map reduce job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.