📅  最后修改于: 2020-11-23 00:59:33             🧑  作者: Mango
Aggregation命令使用聚合管道执行聚合操作。聚合管道允许用户使用基于阶段的应用程序序列从记录或其他源执行数据处理。
句法:
{
aggregate: "" || 1, pipeline: [ , <...>],
explain: , allowDiskUse: ,
cursor: ,
maxTimeMS: ,
bypassDocumentValidation: ,
readConcern: ,
collation: ,
hint: ,
comment: ,
writeConcern:
}
命令字段:
Fields | Type | Description |
---|---|---|
aggregate | string | It contains the name of the aggregation pipeline |
pipeline | array | The array that transforms the list of documents as a part of the aggregation pipeline. |
explain | boolean | The explain field is optional that is used to return the information on the processing of the pipeline. |
allowDiskUse | boolean | It enables the command to write to the temporary files. |
cursor | document | It addresses the documents that contains the control option for the creation of the cursor object. |
maxTimeMS | non-negative integer | It defines a time limit for the processing operations on a cursor. |
Bypass Document Validation |
boolean | It is applicable only in case if you specify the out or merge aggregation stages. |
readConcern | document | It specifies the read concern. The possible read concern levels are – local, available, majority, and linearizable. |
collation | document | We can specify language-specific rules for string comparison using collation. Such as – rules for case and accent marks.
collation: { locale: |
hint | string or document | It declares the index either by the index name or by the index specification document. |
comment | string | We can declare an arbitrary string to help trace the operation through the database profiler, currentOp, and logs. |
writeConcern | document | It is set to use the default write concern with the $out or $merge pipeline stages. |
例:
本文中包含以下文档:
{
_id: ObjectId("52769ea0f3dc6ead47c9a1b2"),
author: "Ankit",
title: "JavaTpoint",
tags: [ "Java Tutorial", "DBMS Tutorial", "mongodb"]
}
现在,我们将对articles集合执行聚合操作,以计算出现在集合中的tags数组中每个不同元素的计数。
db.runCommand( { aggregate: "articles",
pipeline: [
{ $project: { tags: 1 } },
{ $unwind: "$tags" },
{ $group: { _id: "$tags", count: { $sum : 1 } } }
],
cursor: { }
} )
MongoDB count命令对集合或视图中的文档数进行计数。它返回包含计数和命令状态的文档。
句法:
{
count: ,
query: ,
limit: ,
skip: ,
hint: ,
readConcern: ,
collation:
}
命令字段
Field | Type | Description |
---|---|---|
count | string | It is the name of the collection or view to count. |
query | document | It is optional and used to select the document to count in the collection or view. |
limit | integer | It is optional and used to limit the maximum number of matching documents to return. |
skip | integer | It is optional and is used for matching documents to skip before returning results. |
hint | string | It is used to define either the index name as a string or the index specification document. |
readConcerndocument | It specifies the read concern.
readConcern: { level: |
|
collation | document | It allows us to define language-specific rules for string comparison. Syntax: collation: { locale: |
例子:
要计算集合中所有文档的数量:
db.runCommand( { count: 'orders' } )
此命令在单个集合中查找给定字段的不同值。它返回包含不同值数组的文档。返回文档包含带有查询统计信息和查询计划的嵌入式记录。
句法:
distinct: "",
key: "",
query: ,
readConcern: ,
collation:
}
命令字段
Field | Type | Description |
---|---|---|
distinct | string | It is the name of the collection to query for different values |
key | string | This is the field for which the command returns the distinct value. |
query | document | It specifies the documents from where the distinct value will be retrieved. |
readConcerndocument | It specifies the read concern.
readConcern: { level: |
|
collation | document | It allows us to define language-specific rules for string comparison. Syntax: collation: { locale: |
例:
下面的示例从库集合中的所有文档中为外业手簿返回不同的值:
db.runCommand ( { distinct: "library", key: "books" } )
MapReduce命令允许我们在集合上运行map-reduce聚合操作。
句法:
db.runCommand(
{
mapReduce: ,
map: ,
reduce: ,
finalize: ,
out:
指令栏位
Field | Type | Description |
---|---|---|
MapReduce | collection | It is the name of the collection on which we want to perform the map-reduce operation. |
map | function | It is a JavaScript function that associates or maps the key-value pair. |
reduce | function | It is a JavaScript function that reduces to a single object all the values associated with a particular key. |
out | string | It specifies where to store the output. |
query | document | It specifies the selection criteria to determine the documents input to the map function. |
sort | document | It sorts the input document. |
limit | number | It specifies a maximum number of documents for the input into the map function. |
finalize | function | It follows the reduce method to modify the output. |
scope | document | It is an option and declares the global variables that are accessible on the map. |
jsMode | boolean | The jsMode specifies whether to convert intermediate data into BSON format. |
verbose | boolean | It specifies whether to include the timing information in the result or not. |
bypass Document Validation |
boolean | It enables map-reduce to bypass document validation during the operation. |
collation | document | It allows us to specify language-specific rules for string comparison.
collation: { locale: |
writeConcern | document | It is a document that expresses the write concern to use when outputting to a collection. |
例:
var mapFunction = function() { ... };
var reduceFunction = function(key, values) { ... };
db.runCommand(
{
mapReduce: ,
map: mapFunction,
reduce: reduceFunction,
out: { merge: },
query:
}
)