Use aggregations, db. Collection name. Aggregate ... Rather than find.
Pipelines are typically used in UNIX and Linux to use the output of the current command as a parameter to the next command.
MongoDB's aggregation pipeline passes the MongoDB document to the next pipeline processing after a pipeline has finished processing. Pipeline operations can be repeated.
Each operator (the collection) takes a series of documents, makes some type conversions on those documents, and finally passes the converted document as a result to the next operator, and for the last operator, returns the result to the client
Grouping (the field name of the Group field $+ is established here)
As you can see here, Lastmodifybyusercode as a unique identifier _id, the field count calculates the number of occurrences of the same lastmodifybyusercode with $sum
Db.test.aggregate ({' $group ': {' _id ': ' $lastModifyByUserCode ', ' count ': {' $sum ': 1}}})
Grouping by multiple fields
Db.test.aggregate ({$group: {_id:{lastmodifybyusercode: ' $lastModifyByUserCode ', createtime: ' $createTime '}}})
Show additional fields (must use aggregation [$sum, $avg, $first, $last, $max, $min, $push, $addToSet, $stdDevPop, $stdDevSamp])
Db.test.aggregate ({' $group ': {' _id ': ' $lastModifyByUserCode ', ' count ': {' $sum ': 1}, ' Createtime ': {' $first ': ' $ Createtime "}}})
Cast, you can rename the field to create a copy of the field (note: When renaming a field, MongoDB does not record the history name of the field, so if there is an index on the "Orginalfieldname" field, The aggregation framework cannot use this index in the following sort operation, so you should try to use the index before modifying the field name .
Db.test.aggregate ({$project: {createTime2: ' $createTime ', CreateTime3: ' $createTime ', _id:0}})
$match is used to filter the collection of documents, supports find-like filters, cannot use geospatial operators in $match, (as far as possible to put $match in the first of multiple aggregations, so that the index can be effectively used)
And according to official documentation, if full-text indexing is used in $match, then the aggregations that follow are no longer used $text
MongoDB Group Aggregation Query