How does mongodb de-duplicate and count specific fields?

  mongodb, question

This is my document structure:

{
 "_id" : ObjectId("5a159f000cf2ef0dfdfec4b7"),
 "invalidtype" : NumberInt(1),
 "stuMultyLoginStudy" : NumberInt(0),
 "coaMultyLogin" : NumberInt(0),
 "exceedRegion" : NumberInt(0),
 "exceedTime" : NumberInt(1),
 "exceedSimTime" : NumberInt(0),
 "invalidTimePeriod" : NumberInt(0),
 "enginespeedEq0" : NumberInt(0),
 "speedEq0" : NumberInt(0),
 "serialspeedEq0" : NumberInt(0),
 "serialMileageEq0" : NumberInt(0),
 "photoMiss" : NumberInt(0),
 "totalMileagelt100" : NumberInt(0),
 "invalidTime" : NumberInt(0),
 "createtime" : ISODate("2017-11-24T15:00:39.093+0000"),
 "schAccstatus" : NumberInt(0),
 "isAccUpload" : NumberInt(0),
 "accFailReason" : "",
 "failReason": "Exceeded Daily Learning Hours Limit",
 "trnrec" : {
 "_id" : ObjectId("5a159f000cf2ef0dfdfec4b7"),
 "devnum" : "1428435446437541",
 "recno" : "14284354464375411711221438",
 "staflag" : NumberInt(1),
 "rttype" : NumberInt(0),
 "stunum" : "2752445573714083",
 "classid" : NumberLong(1511357506),
 "coachnum" : "7977552582195235",
 "subject" : "1213000000",
 "maxspeed" : 0.4000000059604645,
 "mileage" : 0.0,
 "rectime" : ISODate("2017-11-22T15:59:54.000+0000"),
 "createtime" : ISODate("2017-11-22T16:00:00.261+0000")
 }
 }

I selected hundreds of thousands of data according to the following conditions:

db.collectionName.find({"trnrec.rttype":0,"trnrec.createtime":{ $gte: ISODate("2017-11-23T00:00:00+0800"),  $lt: ISODate("2017-11-24T00:00:00+0800")}})

Finally, I will de-duplicate and count according to the results of the screening according to the field “trnrec.stunum”. how do I write the script?

Distinct returns an array and length gets the length.
The first parameter of distinct is the deduplication field, and the second parameter is the filter condition.

db.collectionName.distinct('trnrec.stunum', {"trnrec.rttype":0,"trnrec.createtime":{ $gte: ISODate("2017-11-23T00:00:00+0800"),  $lt: ISODate("2017-11-24T00:00:00+0800")}}).length

soonfy