Aggregation Commands

MongoDB aggregate command

The aggregate command does the aggregation operation using the aggregation pipeline. The aggregation pipeline allows the user to perform data processing from a record or other source using a stage-based application sequence.

Syntax:

Command fields:

FieldsTypeDescription
aggregatestringIt contains the name of the aggregation pipeline
pipelinearrayThe array that transforms the list of documents as a part of the aggregation pipeline.
explainbooleanThe explain field is optional that is used to return the information on the processing of the pipeline.
allowDiskUsebooleanIt enables the command to write to the temporary files.
cursordocumentIt addresses the documents that contains the control option for the creation of the cursor object.
maxTimeMSnon-negative integerIt defines a time limit for the processing operations on a cursor.
Bypass
Document
Validation
booleanIt is applicable only in case if you specify the out or merge aggregation stages.
readConcerndocumentIt specifies the read concern. The possible read concern levels are - local, available, majority, and linearizable.
collationdocumentWe can specify language-specific rules for string comparison using collation. Such as - rules for case and accent marks.
collation: 
{ locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>}
hintstring or documentIt declares the index either by the index name or by the index specification document.
commentstringWe can declare an arbitrary string to help trace the operation through the database profiler, currentOp, and logs.
writeConcerndocumentIt is set to use the default write concern with the $out or $merge pipeline stages.

Example:

We have the following documents in the article:

Now, we'll perform an aggregate operation on the articles collection to calculate the count of every distinct element within the tags array that appears within the collection.

MongoDB Count command

The MongoDB count command counts the number of documents in a collection or a view. It returns a document that contains the count and command status.

Syntax:

Command fields

FieldTypeDescription
countstringIt is the name of the collection or view to count.
querydocumentIt is optional and used to select the document to count in the collection or view.
limitintegerIt is optional and used to limit the maximum number of matching documents to return.
skipintegerIt is optional and is used for matching documents to skip before returning results.
hintstringIt is used to define either the index name as a string or the index specification document.
readConcern documentIt specifies the read concern.
readConcern: { level: <value> }
collationdocumentIt allows us to define language-specific rules for string comparison.
Syntax:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}

Examples:

To count the number of all the documents in the collection:

MongoDB Distinct Command

This command finds the distinct values for the given field across a single collection. It returns a document that contains an array of different values. The return document contains an embedded record with query statistics and the query plan.

Syntax:

Command fields

FieldTypeDescription
distinctstringIt is the name of the collection to query for different values
keystringThis is the field for which the command returns the distinct value.
querydocumentIt specifies the documents from where the distinct value will be retrieved.
readConcern documentIt specifies the read concern.
readConcern: { level: <value> }
collationdocumentIt allows us to define language-specific rules for string comparison.
Syntax:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}

Example:

The following example returns different values for the field books from all documents in the library collection:

MongoDB MapReduce command

The MapReduce command allows us to run the map-reduce aggregation operations over a collection.

Syntax:

Command Fields

FieldTypeDescription
MapReducecollectionIt is the name of the collection on which we want to perform the map-reduce operation.
mapfunctionIt is a JavaScript function that associates or maps the key-value pair.
reducefunctionIt is a JavaScript function that reduces to a single object all the values associated with a particular key.
outstringIt specifies where to store the output.
querydocumentIt specifies the selection criteria to determine the documents input to the map function.
sortdocumentIt sorts the input document.
limitnumberIt specifies a maximum number of documents for the input into the map function.
finalizefunctionIt follows the reduce method to modify the output.
scopedocumentIt is an option and declares the global variables that are accessible on the map.
jsModebooleanThe jsMode specifies whether to convert intermediate data into BSON format.
verbosebooleanIt specifies whether to include the timing information in the result or not.
bypass
Document
Validation
booleanIt enables map-reduce to bypass document validation during the operation.
collationdocumentIt allows us to specify language-specific rules for string comparison.
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}
writeConcerndocumentIt is a document that expresses the write concern to use when outputting to a collection.

Example:






Latest Courses