mongoDB查詢進階--聚合管道(一)回顧
mongoDB查詢進階--聚合管道(二)回顧mongodb
管道操做符能夠分爲三類:數據庫
參考MongoDB官網:https://docs.mongodb.com/manual/reference/operator/aggregationexpress
本篇主要內容是管道操做符中的表達式操做符。segmentfault
表達式操做符主要用於在管道中構建表達式時使用,使用相似於函數那樣須要參數,主要用於$project操做符中,用於構建表達式,使用方法通常以下:數組
方法1:ide
{ <operator>: [ <argument1>, <argument2> ... ] }
方法2:函數
{ <operator>: <argument> }
操做符 | 簡述 |
---|---|
$and | 邏輯與操做符,當他的表達式中全部值都是true的時候,才返回true。 用法:{ $and: [ <expression1>, <expression2>, ... ] } 。 |
$or | 邏輯或操做符,當他的表達式中有值是true的時候,就會返回true。用法:{ $or: [ <expression1>, <expression2>, ... ] } |
$not | 取反操做符,返回表達式中取反後的布爾值。用法:{ $not: [ <expression> ] } |
例子 | 結果 |
---|---|
{ $and: [ 1, "green" ] } |
true |
{ $and: [ ] } |
true |
{ $and: [ [ null ], [ false ], [ 0 ] ] } |
true |
{ $and: [ null, true ] } |
false |
{ $and: [ 0, true ] } |
false |
{ $or: [ true, false ] } |
true |
{ $or: [ [ false ], false ] } |
true |
{ $or: [ null, 0, undefined ] } |
false |
{ $or: [ ] } |
false |
{ $not: [ true ] } |
false |
{ $not: [ [ false ] ] } |
false |
{ $not: [ false ] } |
true |
{ $not: [ null ] } |
true |
{ $not: [ 0 ] } |
true |
操做符 | 簡述 |
---|---|
$cmp | 比較操做符,比較表達式中兩個值的大小,若是第一個值小於第二個值則返回-1,相等返回0,大於返回1。用法{ $cmp: [ <expression1>, <expression2> ] } |
$eq | 比較表達式中兩個是否相等,是則返回true,不然返回false。用法{ $eq: [ <expression1>, <expression2> ] } |
$gt | 比較表達式中第一個值是否大於第二個值,是則返回true,不然返回false。用法{ $gt: [ <expression1>, <expression2> ] } |
$gte | 比較表達式中第一個值是否大於等於第二個值,是則返回true,不然返回false。用法{ $gte: [ <expression1>, <expression2> ] } |
$lt | 比較表達式中第一個值是否小於第二個值,是則返回true,不然返回false。用法{ $lt: [ <expression1>, <expression2> ] } |
$lte | 比較表達式中第一個值是否小於等於第二個值,是則返回true,不然返回false。用法{ $lte: [ <expression1>, <expression2> ] } |
$ne | 比較表達式中兩個是否相等,不過返回值與$eq相反,是則返回false,不然返回true。用法{ $ne: [ <expression1>, <expression2> ] } |
假設有一個關於考試成績的集合:spa
{ "_id" : 1, "name" : "abc1", score: 80 } { "_id" : 2, "name" : "avc1", score: 82 } { "_id" : 3, "name" : "adc1", score: 79 } { "_id" : 4, "name" : "awc1", score: 60 } { "_id" : 5, "name" : "xyz1", score: 50 } { "_id" : 6, "name" : "VWZ1", score: 100 }
操做以下:code
db.collection.aggregate( [ { $project: { name: 1, score: 1, cmp60: { $cmp: [ "$score", 60 ] }, eq100: { $eq: [ "$score", 100 ] }, gt80: { $gt: [ "$score", 80 ] }, gte80: { $gte: [ "$score", 80 ] }, lt80: { $lt: [ "$score", 80 ] }, lte80: { $lte: [ "$score", 80 ] }, ne100: { $ne: [ "$score", 100 ] }, _id: 0 } } ] )
返回結果:orm
{ "name" : "abc1", score: 80, cmp60: 1, eq100: false, gt80: false, gte80: true, lt80: false, lte80: true, ne100: true } { "name" : "avc1", score: 82, cmp60: 1, eq100: false, gt80: true, gte80: true, lt80: false, lte80: false, ne100: true } { "name" : "adc1", score: 79, cmp60: 1, eq100: false, gt80: false, gte80: false, lt80: true, lte80: false, ne100: true } { "name" : "awc1", score: 60, cmp60: 0, eq100: false, gt80: false, gte80: false, lt80: true, lte80: true, ne100: true } { "name" : "xyz1", score: 50, cmp60: -1, eq100: false, gt80: false, gte80: false, lt80: true, lte80: true, ne100: true } { "name" : "VWZ1", score: 100, cmp60: 1, eq100: true, gt80: true, gte80: true, lt80: false, lte80: false, ne100: false }
操做符 | 簡述 |
---|---|
$abs | 求絕對值操做符,於v3.2版新加入。用法:{ $abs: <number> } |
$add | 求和操做符,返回全部表達式相加起來的結果。用法:{ $add: [ <expression1>, <expression2>, ... ] } |
$ceil | 進一法取整操做符,取 於v3.2版新加入。用法:{ $ceil: <number> } |
$divide | 求商操做符,返回表達式1除以表達式2的商。用法:{ $divide: [ <expression1>, <expression2> ] } |
$subtract | 求差操做符,返回表達式1減去表達式2的結果。用法:{ $subtract: [ <expression1>, <expression2> ] } |
$multiply | 求積操做符,返回全部表達式相乘的結果。用法:{ $multiply: [ <expression1>, <expression2>, ... ] } |
$mod | 求餘操做符,返回全部表達式1除以表達式2所獲得的餘數。用法:{ $multiply: [ <expression1>, <expression2>] } |
例子 | 結果 |
---|---|
{ $abs: -1 } |
1 |
{ $abs: 1 } |
1 |
{ $abs: null } |
null |
{ $add: [1, 1] } |
2 |
{ $ceil: 1 } |
1 |
{ $ceil: 7.80 } |
8 |
{ $ceil: -2.8 } |
-2 |
{ $divide: [40, 8] } |
5 |
{ $subtract: [10, 8] } |
2 |
{ $multiply: [5, 8] } |
40 |
{ $mob: [80, 7] } |
3 |
{ $mob: [80, 8] } |
0 |
Tips:
$add
將一個日期類型和數字類型相加會變成日期類型。 這樣的話,當數據庫存儲的是時間戳可是須要又想對其使用日期操做符的話,就能夠經過這樣的方法,先讓其變成日期類型,而後再使用日期操做符,用法參考:{ $add: [ new Date(0), '$ts' ] }
。
操做符 | 簡述 |
---|---|
$concat | 鏈接操做符,將給定表達式中的字符串鏈接一塊兒。用法:{ $concat: [ <expression1>, <expression2>, ... ] } |
$split | 切割操做符,用於對字符串進行分切。用法:{ $split: [ <string expression>, <delimiter> ] } |
$toLower | 用於返回字符串的小寫形式。用法:{ $toLower: <expression> } |
$toUpper | 用於返回字符串的大寫形式。用法:{ $toUpper: <expression> } |
$substr | 用於返回子字符串,v3.4+版本不建議使用,應該使用substrBytes或substrCP,v3.4+版本使用的話,至關於substrBytes。用法:{ $substr: [ <string>, <start>, <length> ] } |
$substrBytes | 用於根據UTF-8下的字節位置返回子字符串(起始位置爲0),於v3.4新增。用法:{ $substrBytes: [ <string expression>, <byte index>, <byte count> ] } |
$substrCP | 用於根據UTF-8下的Code Point位置返回子字符串(起始位置爲0),於v3.4新增。用法:{ $substrCP: [ <string expression>, <code point index>, <code point count> ] } |
Code Point: (1) Any value in the Unicode codespace; that is, the range of integers from 0 to 10FFFF16. Not all code points are assigned to encoded characters. See code point type. (2) A value, or position, for a character, in any coded character set.
例子 | 結果 |
---|---|
{ $concat: [ "item", " - ", "a" ] } |
item - a |
{ $split: [ "June-15-2013", "-" ] } |
[ "June", "15", "2013" ] |
{ $split: [ "banana split", "a" ] } |
[ "b", "n", "n", " split" ] |
{ $split: [ "headphone jack", 7 ] } |
$split第二個參數必須是一個字符串,不能是數字 |
{ $toLower: "ITEM" } |
"item" |
{ $toLower: "Item" } |
"item" |
{ $toLower: null } |
"" |
{ $toUpper: "item" } |
"ITEM" |
{ $toUpper: "Item" } |
"ITEM" |
{ $toUpper: null } |
"" |
{ $substrBytes: [ "abcde", 1, 2 ] } |
"bc" |
{ $substrBytes: [ "Hello World!", 6, 5 ] } |
"World" |
{ $substrBytes: [ "cafétéria", 0, 5 ] } |
"café" |
{ $substrBytes: [ "cafétéria", 5, 4 ] } |
"tér" |
{ $substrBytes: [ "cafétéria", 7, 3 ] } |
"Error: Invalid range, starting index is a UTF-8 continuation byte." |
{ $substrBytes: [ "cafétéria", 3, 1 ] } |
"Error: Invalid range, ending index is in the middle of a UTF-8 character." |
{ $substrBytes: [ "壽司sushi", 0, 3 ] } |
"壽" |
{ $substrCP: [ "abcde", 1, 2 ] } |
"bc" |
{ $substrCP: [ "Hello World!", 6, 5 ] } |
"World" |
{ $substrCP: [ "cafétéria", 0, 5 ] } |
"cafét" |
{ $substrCP: [ "cafétéria", 5, 4 ] } |
"tér" |
{ $substrCP: [ "cafétéria", 7, 3 ] } |
"ia" |
{ $substrCP: [ "cafétéria", 3, 1 ] } |
"é" |
{ $substrCP: [ "壽司sushi", 0, 3 ] } |
"壽司s" |
操做符 | 簡述 |
---|---|
$dayOfYear | 返回一年中的一天,值在1和366(閏年)之間。用法:{ $dayOfYear: <expression> } |
$dayOfMonth | 返回一個月中的一天,值在1和31之間。用法:{ $dayOfMonth: <expression> } |
$dayOfWeek | 返回一週中的一天,值在1(週日)和7(週六)之間。用法:{ $dayOfWeek: <expression> } |
$year | 返回年份,eg:2017。用法:{ $year: <expression> } |
$month | 返回月份,值在1和12之間。用法:{ $month: <expression> } |
$week | 返回周 ,值在0和53之間。用法:{ $week: <expression> } |
$hour | 返回時 ,值在0和23之間。用法:{ $hour: <expression> } |
$minute | 返回分 ,值在0和59之間。用法:{ $minute: <expression> } |
$second | 返回秒,值在0和60之間(閏秒)。用法:{ $second: <expression> } |
$millisecond | 返回毫秒,值在0和999之間。用法:{ $millisecond: <expression> } |
$dateToString | 返回日期的字符串。用法:{ $dateToString: { format: <formatString>, date: <dateExpression> } } |
假若有如下數據:
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:15:39.736Z") }
進行以下操做:
db.collection.aggregate( [ { $project: { year: { $year: "$date" }, month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, hour: { $hour: "$date" }, minutes: { $minute: "$date" }, seconds: { $second: "$date" }, milliseconds: { $millisecond: "$date" }, dayOfYear: { $dayOfYear: "$date" }, dayOfWeek: { $dayOfWeek: "$date" }, week: { $week: "$date" }, yearMonthDayUTC: { $dateToString: { format: "%Y-%m-%d", date: "$date" } }, time: { $dateToString: { format: "%H:%M:%S:%L", date: "$date" } } } } ] )
返回結果:
{ "_id" : 1, "year" : 2014, "month" : 1, "day" : 1, "hour" : 8, "minutes" : 15, "seconds" : 39, "milliseconds" : 736, "dayOfYear" : 1, "dayOfWeek" : 4, "week" : 0, "yearMonthDayUTC" : "2014-01-01", "time" : "08:15:39:736" }
操做符 | 簡述 |
---|---|
$cond | 用法:{ $cond: [ <boolean-expression>, <true-case>, <false-case> ] } 或者 v2.6+還支持{ $cond: { if: <boolean-expression>, then: <true-case>, else: <false-case-> } } |
$ifNull | 用法:{ $ifNull: [ <expression>, <replacement-expression-if-null> ] } |
假設有一個關於考試成績的集合:
{ "_id" : 1, "name" : "a", score: 80 } { "_id" : 2, "name" : "b", score: 69 } { "_id" : 3, "name" : "c", score: 53 } { "_id" : 3, "name" : null, score: 70 }
操做以下:
db.collection.aggregate( [ { $project: { _id: 0, score: 1, pass: { $cond: [ { $gte: [ "$score", 60 ] }, 1, 0 ] }, description: { $ifNull: [ "$name", "Unspecified" ] } } } ] )
返回結果:
{ "name" : "a", score: 80, pass: 1 } { "name" : "b", score: 69, pass: 1 } { "name" : "c", score: 53, pass: 0 } { "name" : "Unspecified", score: 70, pass: 1 }
本文介紹表達式操做符的分類和經常使用的表達式操做符的用法,表達式操做符主要做用於$project下,經過使用這些操做符能夠對文檔中的字面量進行處理並返回,進而返回更多有用的數據。
感謝閱讀~