Adi Rosenfeld
Adi Rosenfeld

Reputation: 21

How to count distinct values in a MongoDB?

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()

Upvotes: 2

Views: 3049

Answers (1)

nickmilon
nickmilon

Reputation: 1372

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

Upvotes: 3

Related Questions