Mongo query total number after deduplication

  mongodb, question

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


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


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