W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。連接字符串,返回拼接后的字符串。
[<表達(dá)式1>, <表達(dá)式2>, ...]
concat 的語法如下:
db.command.aggregate.concat([<表達(dá)式1>, <表達(dá)式2>, ...])
表達(dá)式可以是形如 $ + 指定字段,也可以是普通字符串。只要能夠被解析成字符串即可。
假設(shè)集合 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 字段,得到每位學(xué)生的名字全稱:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
fullName: $.concat(['$firstName', ' ', '$lastName'])
})
.end()
返回的結(jié)果如下:
{ "fullName": "Yuanxin Dong" }
{ "fullName": "Weijia Wang" }
{ "fullName": "Chengxi Li" }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。將一個(gè)日期/時(shí)間字符串轉(zhuǎn)換為日期對(duì)象
語法如下:
db.command.aggregate.dateFromString({
dateString: <dateStringExpression>,
timezone: <tzExpression>
})
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, 云函數(shù) 0.8.1, Web
聚合操作符。根據(jù)指定的表達(dá)式將日期對(duì)象格式化為符合要求的字符串。
dateToString 的調(diào)用形式如下:
db.command.aggregate.dateToString({
date: <日期表達(dá)式>,
format: <格式化表達(dá)式>,
timezone: <時(shí)區(qū)表達(dá)式>,
onNull: <空值表達(dá)式>
})
下面是四種表達(dá)式的詳細(xì)說明:
名稱 | 描述 |
---|---|
日期表達(dá)式 | 必選。指定字段值應(yīng)該是能轉(zhuǎn)化為字符串的日期。 |
格式化表達(dá)式 | 可選。它可以是任何包含“格式說明符”的有效字符串。 |
時(shí)區(qū)表達(dá)式 | 可選。指明運(yùn)算結(jié)果的時(shí)區(qū)。它可以解析格式為 UTC Offset 或者 Olson Timezone Identifier 的字符串。 |
空值表達(dá)式 | 可選。當(dāng) <日期表達(dá)式> 返回空或者不存在的時(shí)候,會(huì)返回此表達(dá)式指明的值。 |
下面是格式說明符的詳細(xì)說明:
說明符 | 描述 | 合法值 |
---|---|---|
%d | 月份的日期(2位數(shù),0填充) | 01 - 31 |
%G | ISO 8601 格式的年份 | 0000 - 9999 |
%H | 小時(shí)(2位數(shù),0填充,24小時(shí)制) | 00 - 23 |
%j | 一年中的一天(3位數(shù),0填充) | 001 - 366 |
%L | 毫秒(3位數(shù),0填充) | 000 - 999 |
%m | 月份(2位數(shù),0填充) | 01 - 12 |
%M | 分鐘(2位數(shù),0填充) | 00 - 59 |
%S | 秒(2位數(shù),0填充) | 00 - 60 |
%w | 星期幾 | 1 - 7 |
%u | ISO 8601 格式的星期幾 | 1 - 7 |
%U | 一年中的一周(2位數(shù),0填充) | 00 - 53 |
%V | ISO 8601 格式的一年中的一周 | 1 - 53 |
%Y | 年份(4位數(shù),0填充) | 0000 - 9999 |
%z | 與 UTC 的時(shí)區(qū)偏移量 | +/-[hh][mm]
|
%Z | 以分鐘為單位,與 UTC 的時(shí)區(qū)偏移量 | +/-mmm
|
%% | 百分號(hào)作為字符 | %
|
假設(shè)集合 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()
返回的結(jié)果如下:
{ "formatDate": "1999-12-11" }
{ "formatDate": "1998-11-10" }
{ "formatDate": "1997-10-09" }
下面是將 date 字段值格式化為上海時(shí)區(qū)時(shí)間的例子:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
formatDate: $.dateToString({
date: '$date',
format: '%H:%M:%S',
timezone: 'Asia/Shanghai'
})
})
.end()
返回的結(jié)果如下:
{ "formatDate": "00:00:00" }
{ "formatDate": "00:00:00" }
{ "formatDate": "00:00:00" }
當(dāng)指定的 <日期表達(dá)式> 返回空或者不存在的時(shí)候,可以設(shè)置缺失情況下的默認(rèn)值:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
formatDate: $.dateToString({
date: '$empty',
onNull: 'null'
})
})
.end()
返回的結(jié)果如下:
{ "formatDate": "null" }
{ "formatDate": "null" }
{ "formatDate": "null" }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。在目標(biāo)字符串中查找子字符串,并返回第一次出現(xiàn)的 UTF-8 的字節(jié)索引(從0開始)。如果不存在子字符串,返回 -1。
[<目標(biāo)字符串表達(dá)式>, <子字符串表達(dá)式>, <開始位置表達(dá)式>, <結(jié)束位置表達(dá)式>]
indexOfBytes 的語法如下:
db.command.aggregate.indexOfBytes([<目標(biāo)字符串表達(dá)式>, <子字符串表達(dá)式>, <開始位置表達(dá)式>, <結(jié)束位置表達(dá)式>])
下面是 4 種表達(dá)式的詳細(xì)描述:
表達(dá)式 | 描述 |
---|---|
目標(biāo)字符串表達(dá)式 | 任何可以被解析為字符串的表達(dá)式 |
子字符串表達(dá)式 | 任何可以被解析為字符串的表達(dá)式 |
開始位置表達(dá)式 | 任何可以被解析為非負(fù)整數(shù)的表達(dá)式 |
結(jié)束位置表達(dá)式 | 任何可以被解析為非負(fù)整數(shù)的表達(dá)式 |
假設(shè)集合 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()
返回的結(jié)果如下:
{ "aStrIndex": 2 }
{ "aStrIndex": 5 }
{ "aStrIndex": -1 }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。在目標(biāo)字符串中查找子字符串,并返回第一次出現(xiàn)的 UTF-8 的 code point 索引(從0開始)。如果不存在子字符串,返回 -1。
[<目標(biāo)字符串表達(dá)式>, <子字符串表達(dá)式>, <開始位置表達(dá)式>, <結(jié)束位置表達(dá)式>]
code point 是“碼位”,又名“編碼位置”。這里特指 Unicode 包中的碼位,范圍是從0(16進(jìn)制)到10FFFF(16進(jìn)制)。
indexOfCP 的語法如下:
db.command.aggregate.indexOfCP([<目標(biāo)字符串表達(dá)式>, <子字符串表達(dá)式>, <開始位置表達(dá)式>, <結(jié)束位置表達(dá)式>])
下面是 4 種表達(dá)式的詳細(xì)描述:
表達(dá)式 | 描述 |
---|---|
目標(biāo)字符串表達(dá)式 | 任何可以被解析為字符串的表達(dá)式 |
子字符串表達(dá)式 | 任何可以被解析為字符串的表達(dá)式 |
開始位置表達(dá)式 | 任何可以被解析為非負(fù)整數(shù)的表達(dá)式 |
結(jié)束位置表達(dá)式 | 任何可以被解析為非負(fù)整數(shù)的表達(dá)式 |
假設(shè)集合 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()
返回的結(jié)果如下:
{ "aStrIndex": 2 }
{ "aStrIndex": 5 }
{ "aStrIndex": -1 }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。按照分隔符分隔數(shù)組,并且刪除分隔符,返回子字符串組成的數(shù)組。如果字符串無法找到分隔符進(jìn)行分隔,返回原字符串作為數(shù)組的唯一元素。
[<字符串表達(dá)式>, <分隔符表達(dá)式>]
split 的語法如下:
db.command.aggregate.split([<字符串表達(dá)式>, <分隔符表達(dá)式>])
字符串表達(dá)式和分隔符表達(dá)式可以是任意形式的表達(dá)式,只要它可以被解析為字符串即可。
假設(shè)集合 students 的記錄如下:
{ "birthday": "1999/12/12" }
{ "birthday": "1998/11/11" }
{ "birthday": "1997/10/10" }
通過 split 將每條記錄中的 birthday 字段對(duì)應(yīng)值分隔成數(shù)組,每個(gè)數(shù)組分別由代表年、月、日的3個(gè)元素組成:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
birthday: $.split(['$birthday', '/'])
})
.end()
返回的結(jié)果如下:
{ "birthday": [ "1999", "12", "12" ] }
{ "birthday": [ "1998", "11", "11" ] }
{ "birthday": [ "1997", "10", "10" ] }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。計(jì)算并返回指定字符串中 utf-8 編碼的字節(jié)數(shù)量。
表達(dá)式
strLenBytes 的語法如下:
db.command.aggregate.strLenBytes(<表達(dá)式>)
只要表達(dá)式可以被解析成字符串,那么它就是有效表達(dá)式。
假設(shè)集合 students 的記錄如下:
{ "name": "dongyuanxin", "nickname": "心譚" }
借助 strLenBytes 計(jì)算 name 字段和 nickname 字段對(duì)應(yīng)值的字節(jié)長(zhǎng)度:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
nameLength: $.strLenBytes('$name'),
nicknameLength: $.strLenBytes('$nickname')
})
.end()
返回結(jié)果如下:
{ "nameLength": 11, "nicknameLength": 6 }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。計(jì)算并返回指定字符串的UTF-8 code points 數(shù)量。
表達(dá)式
strLenCP 的語法如下:
db.command.aggregate.strLenCP(<表達(dá)式>)
只要表達(dá)式可以被解析成字符串,那么它就是有效表達(dá)式。
假設(shè)集合 students 的記錄如下:
{ "name": "dongyuanxin", "nickname": "心譚" }
借助 strLenCP 計(jì)算 name 字段和 nickname 字段對(duì)應(yīng)值的UTF-8 code points的數(shù)量:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
nameLength: $.strLenCP('$name'),
nicknameLength: $.strLenCP('$nickname')
})
.end()
返回結(jié)果如下:
{ "nameLength": 11, "nicknameLength": 2 }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。對(duì)兩個(gè)字符串在不區(qū)分大小寫的情況下進(jìn)行大小比較,并返回比較的結(jié)果。
[<表達(dá)式1>, <表達(dá)式2>]
strcasecmp 的語法如下:
db.command.aggregate.strcasecmp([<表達(dá)式1>, <表達(dá)式2>])
只要 表達(dá)式1和 表達(dá)式2 可以被解析成字符串,那么它們就是有效的。
返回的比較結(jié)果有1,0和-1三種:
假設(shè)集合 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()
返回結(jié)果如下:
{ "result": 1 }
{ "result": 1 }
{ "result": -1 }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。返回字符串從指定位置開始的指定長(zhǎng)度的子字符串。它是 db.command.aggregate.substrBytes 的別名,更推薦使用后者。
[<表達(dá)式1>, <表達(dá)式2>, <表達(dá)式3>]
substr 的語法如下:
db.command.aggregate.substr([<表達(dá)式1>, <表達(dá)式2>, <表達(dá)式3>])
表達(dá)式1 是任何可以解析為字符串的有效表達(dá)式,表達(dá)式2 和 表達(dá)式3 是任何可以解析為數(shù)字的有效表達(dá)式。
如果 表達(dá)式2 是負(fù)數(shù),返回的結(jié)果為 ""。
如果 表達(dá)式3 是負(fù)數(shù),返回的結(jié)果為從 表達(dá)式2 指定的開始位置以及之后其余部分的子字符串。
假設(shè)集合 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()
返回的結(jié)果如下:
{ "day": "12", "month": "12", "year": "1999" }
{ "day": "11", "month": "11", "year": "1998" }
{ "day": "10", "month": "10", "year": "1997" }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。返回字符串從指定位置開始的指定長(zhǎng)度的子字符串。子字符串是由字符串中指定的 UTF-8 字節(jié)索引的字符開始,長(zhǎng)度為指定的字節(jié)數(shù)。
[<表達(dá)式1>, <表達(dá)式2>, <表達(dá)式3>]
substrBytes 的語法如下:
db.command.aggregate.substrBytes([<表達(dá)式1>, <表達(dá)式2>, <表達(dá)式3>])
表達(dá)式1 是任何可以解析為字符串的有效表達(dá)式,表達(dá)式2 和 表達(dá)式3 是任何可以解析為數(shù)字的有效表達(dá)式。
如果 表達(dá)式2 是負(fù)數(shù),返回的結(jié)果為 ""。
如果 表達(dá)式3 是負(fù)數(shù),返回的結(jié)果為從 表達(dá)式2 指定的開始位置以及之后其余部分的子字符串。
假設(shè)集合 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()
返回的結(jié)果如下:
{ "day": "12", "month": "12", "year": "1999" }
{ "day": "11", "month": "11", "year": "1998" }
{ "day": "10", "month": "10", "year": "1997" }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。返回字符串從指定位置開始的指定長(zhǎng)度的子字符串。子字符串是由字符串中指定的 UTF-8 字節(jié)索引的字符開始,長(zhǎng)度為指定的字節(jié)數(shù)。
[<表達(dá)式1>, <表達(dá)式2>, <表達(dá)式3>]
substrCP 的語法如下:
db.command.aggregate.substrCP([<表達(dá)式1>, <表達(dá)式2>, <表達(dá)式3>])
表達(dá)式1 是任何可以解析為字符串的有效表達(dá)式,表達(dá)式2 和 表達(dá)式3 是任何可以解析為數(shù)字的有效表達(dá)式。
如果 表達(dá)式2 是負(fù)數(shù),返回的結(jié)果為 ""。
如果 表達(dá)式3 是負(fù)數(shù),返回的結(jié)果為從 表達(dá)式2 指定的開始位置以及之后其余部分的子字符串。
假設(shè)集合 students 的記錄如下:
{ "name": "dongyuanxin", "nickname": "心譚" }
借助 substrCP 可以提取 nickname 字段值的第一個(gè)漢字:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
firstCh: $.substrCP(['$nickname', 0, 1])
})
.end()
返回的結(jié)果如下:
{ "firstCh": "心" }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。將字符串轉(zhuǎn)化為小寫并返回。
toLower 的語法如下:
db.command.aggregate.toLower(表達(dá)式)
只要表達(dá)式可以被解析成字符串,那么它就是有效表達(dá)式。例如:$ + 指定字段。
假設(shè)集合 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 的字段值轉(zhuǎn)化為小寫:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
result: $.toLower('$firstName'),
})
.end()
返回的結(jié)果如下:
{ "result": "yuanxin" }
{ "result": "weijia" }
{ "result": "chengxi" }
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。將字符串轉(zhuǎn)化為大寫并返回。
toUpper 的語法如下:
db.command.aggregate.toUpper(表達(dá)式)
只要表達(dá)式可以被解析成字符串,那么它就是有效表達(dá)式。例如:$ + 指定字段。
假設(shè)集合 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 的字段值轉(zhuǎn)化為大寫:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
result: $.toUpper('$lastName'),
})
.end()
返回的結(jié)果如下:
{ "result": "DONG" }
{ "result": "WANG" }
{ "result": "LI" }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: