In MongoDB, aggregation is mainly used to process data, such as the average value, sum, and so on, and return the computed data results. 1 .? The countcount function returns the number of specified sets. Db. mediaCollection. count () 4db. mediaCollection. find ({Publisher: Apress, Type: Book}). count () 12. distinctdist
In MongoDB, aggregation is mainly used to process data, such as the average value, sum, and so on, and return the computed data results. 1 .? The countcount function returns the number of specified sets. Db. mediaCollection. count () 4 db. mediaCollection. find ({Publisher: "Apress", Type: "Book"}). count () 1 2. distinctdist
In MongoDB, aggregation is mainly used to process data, such as the average value, sum, and so on, and return the computed data results. 1 .? The countcount function returns the number of specified sets.
> db. mediaCollection.count()4> db. mediaCollection.find( { Publisher : "Apress", Type: "Book" } ).count()12. The distinctdistinct function is used to remove duplicates and find all different values.
> db. mediaCollection.distinct( "Title")[ "Definitive Guide to MongoDB, the", "Nevermind" ]> db. mediaCollection.distinct ("Tracklist.Title")[ "In Bloom", "Smells like teen spirit" ]3. The groupgroup function is similar to the SQL GROUP BY function, although the syntax is slightly different. The purpose of this command is to return the array of the grouped items. This function has three parameters :? Key, initial ,? Reduce. The key parameter specifies the group type, for example, grouping by title. The initial value of each group reduce call of the initial parameter. When the reduce parameter is put together with similar elements, two parameters are required: The file being traversed and the aggregation counter object.
> db. mediaCollection.group (... {... key: {Title : true},... initial: {Total : 0},... reduce : function (items,prev)... {... prev.Total += 1... }... }... )[ { "Title" : "Definitive Guide to MongoDB, the", "Total" : 1 }, { "Title" : "Nevermind", "Total" : 2 }, { "Title" : null, "Total" : 1 }]Except key, initial ,? Reduce parameter. You can also specify three optional parameters: keyf, cond, and finalize. Currently, the group function cannot be used in the sharding environment. You can use the MapReduce function instead.
Original article address: mongodb aggregation command, thanks to the original author for sharing.