掃二維碼與項目經理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯網交流
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。連接字符串,返回拼接后的字符串。
[<表達式1>, <表達式2>, ...]
concat 的語法如下:
db.command.aggregate.concat([<表達式1>, <表達式2>, ...])
表達式可以是形如 $ + 指定字段,也可以是普通字符串。只要能夠被解析成字符串即可。
假設集合 students 的記錄如下:
{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }
借助 concat 可以拼接 lastName 和 firstName 字段,得到每位學生的名字全稱:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
fullName: $.concat(['$firstName', ' ', '$lastName'])
})
.end()
返回的結果如下:
{ "fullName": "Yuanxin Dong" }
{ "fullName": "Weijia Wang" }
{ "fullName": "Chengxi Li" }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。將一個日期/時間字符串轉換為日期對象
語法如下:
db.command.aggregate.dateFromString({
dateString: ,
timezone:
})
const $ = db.command.aggregate
db
.collection('dates')
.aggregate()
.project({
_id: 0,
date: $.dateFromString({
dateString: "2019-05-14T09:38:51.686Z"
})
})
.end()
輸出如下:
{
"date": ISODate("2019-05-14T09:38:51.686Z")
}
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。根據指定的表達式將日期對象格式化為符合要求的字符串。
dateToString 的調用形式如下:
db.command.aggregate.dateToString({
date: <日期表達式>,
format: <格式化表達式>,
timezone: <時區(qū)表達式>,
onNull: <空值表達式>
})
下面是四種表達式的詳細說明:
| 名稱 | 描述 |
|---|---|
| 日期表達式 | 必選。指定字段值應該是能轉化為字符串的日期。 |
| 格式化表達式 | 可選。它可以是任何包含“格式說明符”的有效字符串。 |
| 時區(qū)表達式 | 可選。指明運算結果的時區(qū)。它可以解析格式為 UTC Offset 或者 Olson Timezone Identifier 的字符串。 |
| 空值表達式 | 可選。當 <日期表達式> 返回空或者不存在的時候,會返回此表達式指明的值。 |
下面是格式說明符的詳細說明:
| 說明符 | 描述 | 合法值 |
|---|---|---|
| %d | 月份的日期(2位數,0填充) | 01 - 31 |
| %G | ISO 8601 格式的年份 | 0000 - 9999 |
| %H | 小時(2位數,0填充,24小時制) | 00 - 23 |
| %j | 一年中的一天(3位數,0填充) | 001 - 366 |
| %L | 毫秒(3位數,0填充) | 000 - 999 |
| %m | 月份(2位數,0填充) | 01 - 12 |
| %M | 分鐘(2位數,0填充) | 00 - 59 |
| %S | 秒(2位數,0填充) | 00 - 60 |
| %w | 星期幾 | 1 - 7 |
| %u | ISO 8601 格式的星期幾 | 1 - 7 |
| %U | 一年中的一周(2位數,0填充) | 00 - 53 |
| %V | ISO 8601 格式的一年中的一周 | 1 - 53 |
| %Y | 年份(4位數,0填充) | 0000 - 9999 |
| %z | 與 UTC 的時區(qū)偏移量 | +/-[hh][mm] |
| %Z | 以分鐘為單位,與 UTC 的時區(qū)偏移量 | +/-mmm |
| %% | 百分號作為字符 | % |
假設集合 students 有如下記錄:
{ "date": "1999-12-11T16:00:00.000Z", "firstName": "Yuanxin", "lastName": "Dong" }
{ "date": "1998-11-10T16:00:00.000Z", "firstName": "Weijia", "lastName": "Wang" }
{ "date": "1997-10-09T16:00:00.000Z", "firstName": "Chengxi", "lastName": "Li" }
下面是將 date 字段的值,格式化成形如 年份-月份-日期 的字符串:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
formatDate: $.dateToString({
date: '$date',
format: '%Y-%m-%d'
})
})
.end()
返回的結果如下:
{ "formatDate": "1999-12-11" }
{ "formatDate": "1998-11-10" }
{ "formatDate": "1997-10-09" }
下面是將 date 字段值格式化為上海時區(qū)時間的例子:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
formatDate: $.dateToString({
date: '$date',
format: '%H:%M:%S',
timezone: 'Asia/Shanghai'
})
})
.end()
返回的結果如下:
{ "formatDate": "00:00:00" }
{ "formatDate": "00:00:00" }
{ "formatDate": "00:00:00" }
當指定的 <日期表達式> 返回空或者不存在的時候,可以設置缺失情況下的默認值:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
formatDate: $.dateToString({
date: '$empty',
onNull: 'null'
})
})
.end()
返回的結果如下:
{ "formatDate": "null" }
{ "formatDate": "null" }
{ "formatDate": "null" }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。在目標字符串中查找子字符串,并返回第一次出現的 UTF-8 的字節(jié)索引(從0開始)。如果不存在子字符串,返回 -1。
[<目標字符串表達式>, <子字符串表達式>, <開始位置表達式>, <結束位置表達式>]
indexOfBytes 的語法如下:
db.command.aggregate.indexOfBytes([<目標字符串表達式>, <子字符串表達式>, <開始位置表達式>, <結束位置表達式>])
下面是 4 種表達式的詳細描述:
| 表達式 | 描述 |
|---|---|
| 目標字符串表達式 | 任何可以被解析為字符串的表達式 |
| 子字符串表達式 | 任何可以被解析為字符串的表達式 |
| 開始位置表達式 | 任何可以被解析為非負整數的表達式 |
| 結束位置表達式 | 任何可以被解析為非負整數的表達式 |
假設集合 students 的記錄如下:
{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }
借助 indexOfBytes 查找字符 "a" 在字段 firstName 中的位置:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
aStrIndex: $.indexOfBytes(['$firstName', 'a'])
})
.end()
返回的結果如下:
{ "aStrIndex": 2 }
{ "aStrIndex": 5 }
{ "aStrIndex": -1 }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。在目標字符串中查找子字符串,并返回第一次出現的 UTF-8 的 code point 索引(從0開始)。如果不存在子字符串,返回 -1。
[<目標字符串表達式>, <子字符串表達式>, <開始位置表達式>, <結束位置表達式>]
code point 是“碼位”,又名“編碼位置”。這里特指 Unicode 包中的碼位,范圍是從0(16進制)到10FFFF(16進制)。
indexOfCP 的語法如下:
db.command.aggregate.indexOfCP([<目標字符串表達式>, <子字符串表達式>, <開始位置表達式>, <結束位置表達式>])
下面是 4 種表達式的詳細描述:
| 表達式 | 描述 |
|---|---|
| 目標字符串表達式 | 任何可以被解析為字符串的表達式 |
| 子字符串表達式 | 任何可以被解析為字符串的表達式 |
| 開始位置表達式 | 任何可以被解析為非負整數的表達式 |
| 結束位置表達式 | 任何可以被解析為非負整數的表達式 |
假設集合 students 的記錄如下:
{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }
借助 indexOfCP 查找字符 "a" 在字段 firstName 中的位置:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
aStrIndex: $.indexOfCP(['$firstName', 'a'])
})
.end()
返回的結果如下:
{ "aStrIndex": 2 }
{ "aStrIndex": 5 }
{ "aStrIndex": -1 }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。按照分隔符分隔數組,并且刪除分隔符,返回子字符串組成的數組。如果字符串無法找到分隔符進行分隔,返回原字符串作為數組的唯一元素。
[<字符串表達式>, <分隔符表達式>]
split 的語法如下:
db.command.aggregate.split([<字符串表達式>, <分隔符表達式>])
字符串表達式和分隔符表達式可以是任意形式的表達式,只要它可以被解析為字符串即可。
假設集合 students 的記錄如下:
{ "birthday": "1999/12/12" }
{ "birthday": "1998/11/11" }
{ "birthday": "1997/10/10" }
通過 split 將每條記錄中的 birthday 字段對應值分隔成數組,每個數組分別由代表年、月、日的3個元素組成:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
birthday: $.split(['$birthday', '/'])
})
.end()
返回的結果如下:
{ "birthday": [ "1999", "12", "12" ] }
{ "birthday": [ "1998", "11", "11" ] }
{ "birthday": [ "1997", "10", "10" ] }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。計算并返回指定字符串中 utf-8 編碼的字節(jié)數量。
表達式
strLenBytes 的語法如下:
db.command.aggregate.strLenBytes(<表達式>)
只要表達式可以被解析成字符串,那么它就是有效表達式。
假設集合 students 的記錄如下:
{ "name": "dongyuanxin", "nickname": "心譚" }
借助 strLenBytes 計算 name 字段和 nickname 字段對應值的字節(jié)長度:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
nameLength: $.strLenBytes('$name'),
nicknameLength: $.strLenBytes('$nickname')
})
.end()
返回結果如下:
{ "nameLength": 11, "nicknameLength": 6 }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。計算并返回指定字符串的UTF-8 code points 數量。
表達式
strLenCP 的語法如下:
db.command.aggregate.strLenCP(<表達式>)
只要表達式可以被解析成字符串,那么它就是有效表達式。
假設集合 students 的記錄如下:
{ "name": "dongyuanxin", "nickname": "心譚" }
借助 strLenCP 計算 name 字段和 nickname 字段對應值的UTF-8 code points的數量:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
nameLength: $.strLenCP('$name'),
nicknameLength: $.strLenCP('$nickname')
})
.end()
返回結果如下:
{ "nameLength": 11, "nicknameLength": 2 }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。對兩個字符串在不區(qū)分大小寫的情況下進行大小比較,并返回比較的結果。
[<表達式1>, <表達式2>]
strcasecmp 的語法如下:
db.command.aggregate.strcasecmp([<表達式1>, <表達式2>])
只要 表達式1和 表達式2 可以被解析成字符串,那么它們就是有效的。
返回的比較結果有1,0和-1三種:
假設集合 students 的記錄如下:
{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }
借助 strcasecmp 比較 firstName 字段值和 lastName 字段值的大?。?/p>
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
result: $.strcasecmp(['$firstName', '$lastName']),
})
.end()
返回結果如下:
{ "result": 1 }
{ "result": 1 }
{ "result": -1 }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。返回字符串從指定位置開始的指定長度的子字符串。它是 db.command.aggregate.substrBytes 的別名,更推薦使用后者。
[<表達式1>, <表達式2>, <表達式3>]
substr 的語法如下:
db.command.aggregate.substr([<表達式1>, <表達式2>, <表達式3>])
表達式1 是任何可以解析為字符串的有效表達式,表達式2 和 表達式3 是任何可以解析為數字的有效表達式。
如果 表達式2 是負數,返回的結果為 ""。
如果 表達式3 是負數,返回的結果為從 表達式2 指定的開始位置以及之后其余部分的子字符串。
假設集合 students 的記錄如下:
{ "birthday": "1999/12/12", "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "birthday": "1998/11/11", "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "birthday": "1997/10/10", "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }
借助 substr 可以提取 birthday 中的年、月、日信息,代碼如下:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
year: $.substr(['$birthday', 0, 4]),
month: $.substr(['$birthday', 5, 2]),
day: $.substr(['$birthday', 8, -1])
})
.end()
返回的結果如下:
{ "day": "12", "month": "12", "year": "1999" }
{ "day": "11", "month": "11", "year": "1998" }
{ "day": "10", "month": "10", "year": "1997" }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。返回字符串從指定位置開始的指定長度的子字符串。子字符串是由字符串中指定的 UTF-8 字節(jié)索引的字符開始,長度為指定的字節(jié)數。
[<表達式1>, <表達式2>, <表達式3>]
substrBytes 的語法如下:
db.command.aggregate.substrBytes([<表達式1>, <表達式2>, <表達式3>])
表達式1 是任何可以解析為字符串的有效表達式,表達式2 和 表達式3 是任何可以解析為數字的有效表達式。
如果 表達式2 是負數,返回的結果為 ""。
如果 表達式3 是負數,返回的結果為從 表達式2 指定的開始位置以及之后其余部分的子字符串。
假設集合 students 的記錄如下:
{ "birthday": "1999/12/12", "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "birthday": "1998/11/11", "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "birthday": "1997/10/10", "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }
借助 substrBytes 可以提取 birthday 中的年、月、日信息,代碼如下:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
year: $.substrBytes(['$birthday', 0, 4]),
month: $.substrBytes(['$birthday', 5, 2]),
day: $.substrBytes(['$birthday', 8, -1])
})
.end()
返回的結果如下:
{ "day": "12", "month": "12", "year": "1999" }
{ "day": "11", "month": "11", "year": "1998" }
{ "day": "10", "month": "10", "year": "1997" }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。返回字符串從指定位置開始的指定長度的子字符串。子字符串是由字符串中指定的 UTF-8 字節(jié)索引的字符開始,長度為指定的字節(jié)數。
[<表達式1>, <表達式2>, <表達式3>]
substrCP 的語法如下:
db.command.aggregate.substrCP([<表達式1>, <表達式2>, <表達式3>])
表達式1 是任何可以解析為字符串的有效表達式,表達式2 和 表達式3 是任何可以解析為數字的有效表達式。
如果 表達式2 是負數,返回的結果為 ""。
如果 表達式3 是負數,返回的結果為從 表達式2 指定的開始位置以及之后其余部分的子字符串。
假設集合 students 的記錄如下:
{ "name": "dongyuanxin", "nickname": "心譚" }
借助 substrCP 可以提取 nickname 字段值的第一個漢字:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
firstCh: $.substrCP(['$nickname', 0, 1])
})
.end()
返回的結果如下:
{ "firstCh": "心" }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。將字符串轉化為小寫并返回。
toLower 的語法如下:
db.command.aggregate.toLower(表達式)
只要表達式可以被解析成字符串,那么它就是有效表達式。例如:$ + 指定字段。
假設集合 students 的記錄如下:
{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }
借助 toLower 將 firstName 的字段值轉化為小寫:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
result: $.toLower('$firstName'),
})
.end()
返回的結果如下:
{ "result": "yuanxin" }
{ "result": "weijia" }
{ "result": "chengxi" }
支持端:小程序 2.7.4, 云函數 0.8.1, Web
聚合操作符。將字符串轉化為大寫并返回。
toUpper 的語法如下:
db.command.aggregate.toUpper(表達式)
只要表達式可以被解析成字符串,那么它就是有效表達式。例如:$ + 指定字段。
假設集合 students 的記錄如下:
{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }
借助 toUpper 將 lastName 的字段值轉化為大寫:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
result: $.toUpper('$lastName'),
})
.end()
返回的結果如下:
{ "result": "DONG" }
{ "result": "WANG" }
{ "result": "LI" } 
我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯網交流