SDK数据库 Command聚合操作符字符串操作符


本文整理自网络,侵删。

AggregateCommand.concat(value: Expression[]): Object

支持端:小程序 2.7.4, 云函数 0.8.1, Web

聚合操作符。连接字符串,返回拼接后的字符串。

参数

value: Expression[]

[<表达式1>, <表达式2>, ...]

返回值

Object

API 说明

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" }

AggregateCommand.dateFromString(value: any): Object

支持端:小程序 2.7.4, 云函数 0.8.1, Web

聚合操作符。将一个日期/时间字符串转换为日期对象

参数

value: any

返回值

Object

API 说明

语法如下:

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")
}

AggregateCommand.dateToString(value: any): Object

支持端:小程序 2.7.4, 云函数 0.8.1, Web

聚合操作符。根据指定的表达式将日期对象格式化为符合要求的字符串。

参数

value: any

返回值

Object

API 说明

dateToString 的调用形式如下:

db.command.aggregate.dateToString({
  date: <日期表达式>,
  format: <格式化表达式>,
  timezone: <时区表达式>,
  onNull: <空值表达式>
})

下面是四种表达式的详细说明:

名称 描述
日期表达式 必选。指定字段值应该是能转化为字符串的日期。
格式化表达式 可选。它可以是任何包含“格式说明符”的有效字符串。
时区表达式 可选。指明运算结果的时区。它可以解析格式为 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 的时区偏移量 +/-[hh][mm]
%Z 以分钟为单位,与 UTC 的时区偏移量 +/-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 字段值格式化为上海时区时间的例子:

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" }

AggregateCommand.indexOfBytes(value: Expression[]): Object

支持端:小程序 2.7.4, 云函数 0.8.1, Web

聚合操作符。在目标字符串中查找子字符串,并返回第一次出现的 UTF-8 的字节索引(从0开始)。如果不存在子字符串,返回 -1。

参数

value: Expression[]

[<目标字符串表达式>, <子字符串表达式>, <开始位置表达式>, <结束位置表达式>]

返回值

Object

API 说明

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 }

AggregateCommand.indexOfCP(value: Expression[]): Object

支持端:小程序 2.7.4, 云函数 0.8.1, Web

聚合操作符。在目标字符串中查找子字符串,并返回第一次出现的 UTF-8 的 code point 索引(从0开始)。如果不存在子字符串,返回 -1。

参数

value: Expression[]

[<目标字符串表达式>, <子字符串表达式>, <开始位置表达式>, <结束位置表达式>]

返回值

Object

API 说明

code point 是“码位”,又名“编码位置”。这里特指 Unicode 包中的码位,范围是从0(16进制)到10FFFF(16进制)。

阅读剩余部分

相关阅读 >>

微信小程序设计规范(1) 友好礼貌

微信小程序 使用插件

微信小程序api wx.updatewechatapp

微信小程序云开发sdk文档 微信支付关闭订单

微信小程序 服务平台api

微信小程序 运力方使用onorderpreadd

微信小程序 小程序使用getorder

微信小程序 订阅消息gettemplatelist

微信小程序api 设置置顶信息

微信小程序 wi-fi

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...