我們先介紹一下 MongoDB 的聚合功能,聚合操作主要用於對數據的批量處理,往往將記錄按條件分組以後,然後再進行一系列操作,例如,求最大值、最小值、平均值,求和等操作。聚合操作還能夠對記錄進行複雜的操作,主要用於數理統計和數據挖掘。在 MongoDB 中,聚合操作的輸入是集合中的文檔,輸出可以是一 ...
我們先介紹一下 MongoDB 的聚合功能,聚合操作主要用於對數據的批量處理,往往將記錄按條件分組以後,然後再進行一系列操作,例如,求最大值、最小值、平均值,求和等操作。聚合操作還能夠對記錄進行複雜的操作,主要用於數理統計和數據挖掘。在 MongoDB 中,聚合操作的輸入是集合中的文檔,輸出可以是一個文檔,也可以是多條文檔。在管道查詢過程中,上次查詢的結果可以為這次查詢的條件。
使用階段操作符之前,我們先看一下 article 集合中的文檔列表,也就是範例中用到的數據。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1.1.1、$project
作用
修改文檔的結構,可以用來重命名、增加或刪除文檔中的欄位。
範例1
只返迴文檔中 title 和 author 欄位
1 2 3 4 |
|
因為欄位 _id 是預設顯示的,這裡必須用 _id:0 把欄位_id過濾掉。
範例2
把文檔中 pages 欄位的值都增加10。並重命名成 newPages 欄位。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
其中,$add 是 加 的意思,是算術類型表達式操作符,具體表達式操作符,下麵會講到。
1.1.2、$match
作用
用於過濾文檔。用法類似於 find() 方法中的參數。
範例
查詢出文檔中 pages 欄位的值大於等於5的數據。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
註:
- 在 $match 中不能使用 $where 表達式操作符
- 如果 $match 位於管道的第一個階段,可以利用索引來提高查詢效率
- $match 中使用 $text 操作符的話,只能位於管道的第一階段
- $match 儘量出現在管道的最前面,過濾出需要的數據,在後續的階段中可以提高效率。
1.1.3、$group
作用
將集合中的文檔進行分組,可用於統計結果。
範例
從 article 中得到每個 author 的文章數,並輸入 author 和對應的文章數。
1 2 3 4 5 6 7 8 9 |
|
1.1.4、$sort
作用
將集合中的文檔進行排序。
範例
讓集合 article 以 pages 升序排列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
如果以降序排列,則設置成 "pages": -1
1.1.5、$limit
作用
限制返回的文檔數量
範例
返回集合 article 中前兩條文檔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1.1.6、$skip
作用
跳過指定數量的文檔,並返回餘下的文檔。
範例
跳過集合 article 中一條文檔,輸出剩下的文檔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1.1.7、$unwind
作用
將文檔中數組類型的欄位拆分成多條,每條文檔包含數組中的一個值。
範例
把集合 article 中 title="MongoDB Aggregate" 的 tags 欄位拆分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
註:
- $unwind 參數數組欄位為空或不存在時,待處理的文檔將會被忽略,該文檔將不會有任何輸出
- $unwind 參數不是一個數組類型時,將會拋出異常
- $unwind 所作的修改,只用於輸出,不能改變原文檔
1.2、表達式操作符
表達式操作符有很多操作類型,其中最常用的有布爾管道聚合操作、集合操作、比較聚合操作、算術聚合操作、字元串聚合操作、數組聚合操作、日期聚合操作、條件聚合操作、數據類型聚合操作等。每種類型都有很多用法,這裡就不一一舉例了。
1.2.1、布爾管道聚合操作(Boolean Aggregation Operators)
名稱 | 說明 |
---|---|
$and |
Returns true only when all its expressions evaluate to true . Accepts any number of argument expressions. |
$or |
Returns true when any of its expressions evaluates to true . Accepts any number of argument expressions. |
$not |
Returns the boolean value that is the opposite of its argument expression. Accepts a single argument expression. |
範例
假如有一個集合 mycol
1 2 3 4 5 |
|
確定 qty 是否大於250或者小於200
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1.2.2、集合操作(Set Operators)
用於集合操作,求集合的並集、交集、差集運算。
名稱 | 說明 |
---|---|
$setEquals |
Returns true if the input sets have the same distinct elements. Accepts two or more argument expressions. |
$setIntersection |
Returns a set with elements that appear in all of the input sets. Accepts any number of argument expressions. |
$setUnion |
Returns a set with elements that appear in any of the input sets. Accepts any number of argument expressions. |
$setDifference |
Returns a set with elements that appear in the first set but not in the second set; i.e. performs a relative complement of the second set relative to the first. Accepts exactly two argument expressions. |
$setIsSubset |
Returns true if all elements of the first set appear in the second set, including when the first set equals the second set; i.e. not a strict subset. Accepts exactly two argument expressions. |
$anyElementTrue |
Returns true if any elements of a set evaluate to true ; otherwise, returns false . Accepts a single argument expression. |
$allElementsTrue |
Returns true if no element of a set evaluates to false , otherwise, returns false . Accepts a single argument expression. |
範例
假如有一個集合 mycol
1 2 3 4 5 6 7 8 9 |
|
求出集合 mycol 中 A 和 B 的交集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1.2.3、比較聚合操作(Comparison Aggregation Operators)
名稱 | 說明 |
---|---|
$cmp |
Returns: 0 if the two values are equivalent, 1 if the first value is greater than the second, and -1 if the first value is less than the second. |
$eq |
Returns true if the values are equivalent. |
$gt |
Returns true if the first value is greater than the second. |
$gte |
Returns true if the first value is greater than or equal to the second. |
$lt |
Returns true if the first value is less than the second. |
$lte |
Returns true if the first value is less than or equal to the second. |
$ne |
Returns true if the values are not equivalent. |
這裡就不舉例了,之前的例子有用到過。
1.2.4、算術聚合操作(Arithmetic Aggregation Operators)
名稱 | 說明 |
---|---|
$abs |
Returns the absolute value of a number. |
$add |
Adds numbers to return the sum, or adds numbers and a date to return a new date. If adding numbers and a date, treats the numbers as milliseconds. Accepts any number of argument expressions, but at most, one expression can resolve to a date. |
$ceil |
Returns the smallest integer greater than or equal to the specified number. |
$divide |
Returns the result of dividing the first number by the second. Accepts two argument expressions. |
$exp |
Raises e to the specified exponent. |
$floor |
Returns the largest integer less than or equal to the specified number. |
$ln |
Calculates the natural log of a number. |
$log |
Calculates the log of a number in the specified base. |
$log10 |
Calculates the log base 10 of a number. |
$mod |
Returns the remainder of the first number divided by the second. Accepts two argument expressions. |
$multiply |
Multiplies numbers to return the product. Accepts any number of argument expressions. |
$pow |
Raises a number to the specified exponent. |
$sqrt |
Calculates the square root. |
$subtract |
Returns the result of subtracting the second value from the first. If the two values are numbers, return the difference. If the two values are dates, return the difference in milliseconds. If the two values are a date and a number in milliseconds, return the resulting date. Accepts two argument expressions. If the two values are a date and a number, specify the date argument first as it is not meaningful to subtract a date from a number. |
$trunc |
Truncates a number to its integer. |
範例
假如有一個集合 mycol
1 2 3 4 |
|
求集合 mycol 中 start 減去 end 的絕對值
1 2 3 4 5 6 7 8 9 |
|
1.2.5、字元串聚合操作(String Aggregation Operators)
名稱 | 說明 |
---|---|
$concat |
Concatenates any number of strings. |
$indexOfBytes |
Searches a string for an occurence of a substring and returns the UTF-8 byte index of the first occurence. If the substring is not found, returns -1 . |
$indexOfCP |
Searches a string for an occurence of a substring and returns the UTF-8 code point index of the first occurence. If the substring is not found, returns -1 . |
$split |
Splits a string into substrings based on a delimiter. Returns an array of substrings. If the delimiter is not found within the string, returns an array containing the original string. |
$strLenBytes |
Returns the number of UTF-8 encoded bytes in a string. |
$strLenCP |
Returns the number of UTF-8 code points in a string. |
$strcasecmp |
Performs case-insensitive string comparison and returns: 0 if two strings are equivalent, 1 if the first string is greater than the second, and -1 if the first string is less than the second. |
$substr |
Deprecated. Use $substrBytes or $substrCP . |
$substrBytes |
Returns the substring of a string. Starts with the character at the specified UTF-8 byte index (zero-based) in the string and continues for the specified number of bytes. |
$substrCP |
Returns the substring of a string. Starts with the character at the specified UTF-8 code point (CP) index (zero-based) in the string and continues for the number of code points specified. |
$toLower |
Converts a string to lowercase. Accepts a single argument expression. |
$toUpper |
Converts a string to uppercase. Accepts a single argument expression. |
範例
假如有一個集合 mycol
1 2 3 4 5 6 7 |
|
以 ',' 分割集合 mycol 中字元串city的值,用 $unwind 拆分成多個文檔,匹配出城市名稱只有兩個字母的城市,並求和各個城市中 qty 的值,最後以降序排序。
1 2 3 4 5 6 7 8 9 10 |
|
1.2.6、數組聚合操作(Array Aggregation Operators)
名稱 | 說明 |
---|---|
$arrayElemAt |
Returns the element at the specified array index. |
$concatArrays |
Concatenates arrays to return the concatenated array. |
$filter |
Selects a subset of the array to return an array with only the elements that match the filter condition. |
$indexOfArray |
Searches an array for an occurence of a specified value and returns the array index of the first occurence. If the substring is not found, returns -1 . |
$isArray |
Determines if the operand is an array. Returns a boolean. |
$range |
Outputs an array containing a sequence of integers according to user-defined inputs. |
$reverseArray |
Returns an array with the elements in reverse order. |
$reduce |
Applies an expression to each element in an array and combines them into a single value. |
$size |
Returns the number of elements in the array. Accepts a single expression as argument. |
$slice |
Returns a subset of an array. |
$zip |
Merge two lists together. |
$in |
Returns a boolean indicating whether a specified value is in an array. |
範例
假如有一個集合 mycol
1 2 3 4 |
|
求出集合 mycol 中 favorites 的第一項和最後一項
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1.2.7、日期聚合操作(Date Aggregation Operators)
名稱 | 說明 |
---|---|
$dayOfYear |
Returns the day of the year for a date as a number between 1 and 366 (leap year). |
$dayOfMonth |
Returns the day of the month for a date as a number between 1 and 31. |
$dayOfWeek |
Returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday). |
$year |
Returns the year for a date as a number (e.g. 2014). |
$month |
Returns the month for a date as a number between 1 (January) and 12 (December). |
$week |
Returns the week number for a date as a number between 0 (the partial week that precedes the first Sunday of the year) and 53 (leap year). |
$hour |
Returns the hour for a date as a number between 0 and 23. |
$minute |
Returns the minute for a date as a number between 0 and 59. |
$second |
Returns the seconds for a date as a number between 0 and 60 (leap seconds). |
$millisecond |
Returns the milliseconds of a date as a number between 0 and 999. |
$dateToString |
Returns the date as a formatted string. |
$isoDayOfWeek |
Returns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday). |
$isoWeek |
Returns the week number in ISO 8601 format, ranging from 1 to 53 . Week numbers start at 1 with the week (Monday through Sunday) that contains the year’s first Thursday. |
$isoWeekYear |
Returns the year number in ISO 8601 format. The year starts with the Monday of week 1 (ISO 8601) and ends with the Sunday of the last week (ISO 8601). |
範例
假如有一個集合 mycol
1 |
|
得到集合 mycol 中 date 欄位的相關日期值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
1.2.8、條件聚合操作(Conditional Aggregation Operators)
名稱 | 說明 |
---|---|
$cond |
A ternary operator that evaluates one expression, and depending on the result, returns the value of one of the other two expressions. Accepts either three expressions in an ordered list or three named parameters. |
$ifNull |
Returns either the non-null result of the first expression or the result of the second expression if the first expression results in a null result. Null result encompasses instances of undefined values or missing fields. Accepts two expressions as arguments. The result of the second expression can be null. |
$switch |
Evaluates a series of case expressions. When it finds an expression which evaluates to true , $switch executes a specified expression and breaks out of the control flow. |
範例
假如有一個集合 mycol
1 2 3 |
|
如果集合 mycol 中 qty 欄位值大於等於250,則返回30,否則返回20