Mongo query total number after deduplication

  mongodb, question

MongoThere are a number of data in the, the general structure is as follows:

{"userId":1,"loginTime":ISODate("2017-11-28T10:47:57.525+08:00")}
 {"userId":2,"loginTime":ISODate("2017-11-28T11:47:57.525+08:00")}
 {"userId":1,"loginTime":ISODate("2017-11-28T12:47:57.525+08:00")}
 {"userId":3,"loginTime":ISODate("2017-11-28T13:47:57.525+08:00")}

To pressuserIdAfter the field is de-duplicated, the total number is counted, and the result returned by the sample data should be: 3, inSHELLCan be used in

db.logins.distinct("userId").length

To query, but inspring-data-mongoUsed inMongoTemplateHow to realize the same function, be careful not to use itdistinctFind out all the results and get themsize, the actual order of magnitude may be larger, such as ten million level.

In addition, I heard thatMongoIndistinctThe result set cannot exceed16MB(Not sure), then aboveSHELLThere should also be problems with the usage of.

The whole requirement can be simply described as: query the daily login number (note that it is not the number of people)

I have tested some usage, but none of them can meet the above requirements:

//actually return a List, so it is definitely not economical to calculate the total
 mongoTemplate.getCollection(collectionName).distinct("userId")