DB
参数
Database
云开发 SDK 数据库实例
| 参数 | 类型 | 说明 |
|---|---|---|
| config | IConfig | 数据库配置 |
| command | Command | 数据库操作符,通过 db.command 获取 API 支持度: weapp 参考地址 |
| Geo | IGeo | 数据库地理位置结构集 API 支持度: weapp 参考地址 |
serverDate
构造一个服务端时间的引用。可用于查询条件、更新字段值或新增记录时的字段值。
支持情况:

(options?: IOptions) => ServerDate
| 参数 | 类型 |
|---|---|
| options | IOptions |
示例代码
新增记录时设置字段为服务端时间:
db.collection('todos').add({
description: 'eat an apple',
createTime: db.serverDate()
})
更新字段为服务端时间往后一小时:
db.collection('todos').doc('my-todo-id').update({
due: db.serverDate({
offset: 60 * 60 * 1000
})
})
RegExp
构造正则表达式,仅需在普通 js 正则表达式无法满足的情况下使用
支持情况:

(options: IRegExpOptions) => IRegExp
| 参数 | 类型 |
|---|---|
| options | IRegExpOptions |
示例代码
// 原生 JavaScript 对象
db.collection('todos').where({
description: /miniprogram/i
})
// 数据库正则对象
db.collection('todos').where({
description: db.RegExp({
regexp: 'miniprogram',
options: 'i',
})
})
// 用 new 构造也是可以的
db.collection('todos').where({
description: new db.RegExp({
regexp: 'miniprogram',
options: 'i',
})
})
collection
获取集合的引用。方法接受一个 name 参数,指定需引用的集合名称。
支持情况:

(collectionName: string) => Collection
| 参数 | 类型 |
|---|---|
| collectionName | string |
示例代码
const db = Taro.cloud.database()
const todosCollection = db.collection('todos')
ServerDate
可用于查询条件、更新字段值或新增记录时的字段值。
| 参数 | 类型 |
|---|---|
| options | IOptions |
IOptions
| 参数 | 类型 |
|---|---|
| offset | number |
IRegExp
构造正则表达式
| 参数 | 类型 |
|---|---|
| regexp | string |
| options | string |
IRegExpOptions
| 参数 | 类型 | 必填 |
|---|---|---|
| regexp | string | 是 |
| options | string | 否 |
InternalSymbol
内部符号
Collection
数据库集合引用
| 参数 | 类型 | 说明 |
|---|---|---|
| collectionName | string | 集合名称 |
| database | Database | 集合所在数据库引用 |
doc
获取集合中指定记录的引用。方法接受一个 id 参数,指定需引用的记录的 _id。
支持情况:

(docId: string | number) => Document
| 参数 | 类型 | 说明 |
|---|---|---|
| docId | string or number | 记录 _id |
示例代码
const myTodo = db.collection('todos').doc('my-todo-id')
aggregate
发起聚合操作,定义完聚合流水线阶段之后需调用 end 方法标志结束定义并实际发起聚合操作
支持情况:

() => Aggregate
示例代码
示例 1
const $ = db.command.aggregate
db.collection('books').aggregate()
.group({
// 按 category 字段分组
_id: '$category',
// 让输出的每组记录有一个 avgSales 字段,其值是组内所有记录的 sales 字段的平均值
avgSales: $.avg('$sales')
})
.end()
.then(res => console.log(res))
.catch(err => console.error(err))
示例 2
const $ = db.command.aggregate
db.collection('books').aggregate()
.group({
// 按 category 字段分组
_id: '$category',
// 让输出的每组记录有一个 avgSales 字段,其值是组内所有记录的 sales 字段的平均值
avgSales: $.avg('$sales')
})
.end({
success: function(res) {
console.log(res)
},
fail: function(err) {
console.error(err)
}
})
where
指定查询条件,返回带新查询条件的新的集合引用
支持情况:

(condition: IQueryCondition) => Collection
| 参数 | 类型 |
|---|---|
| condition | IQueryCondition |
示例代码
const _ = db.command
const result = await db.collection('todos').where({
price: _.lt(100)
}).get()
limit
指定查询结果集数量上限
支持情况:

(value: number) => Collection
| 参数 | 类型 |
|---|---|
| value | number |
示例代码
db.collection('todos').limit(10)
.get()
.then(console.log)
.catch(console.error)
orderBy
指定查询排序条件
支持情况:

(fieldPath: string, string: "asc" | "desc") => Collection
| 参数 | 类型 |
|---|---|
| fieldPath | string |
| string | "asc" or "desc" |
示例代码
按一个字段排序:按进度排升序取待办事项
db.collection('todos').orderBy('progress', 'asc')
.get()
.then(console.log)
.catch(console.error)
按多个字段排序:先按 progress 排降序(progress 越大越靠前)、再按 description 排升序(字母序越前越靠前)取待办事项
db.collection('todos')
.orderBy('progress', 'desc')
.orderBy('description', 'asc')
.get()
.then(console.log)
.catch(console.error)
skip
指定查询返回结果时从指定序列后的结果开始返回,常用于分页
支持情况:

(offset: number) => Collection
| 参数 | 类型 |
|---|---|
| offset | number |
示例代码
db.collection('todos').skip(10)
.get()
.then(console.log)
.catch(console.error)
field
指定返回结果中记录需返回的字段
说明
方法接受一个必填对象用于指定需返回的字段,对象的各个 key 表示要返回或不要返回的字段,value 传入 true|false(或 1|-1)表示要返回还是不要返回。
如果指定的字段是数组字段,还可以用以下方法只返回数组的第一个元素:在该字段 key 后面拼接上 .$ 成为 字段.$ 的形式。
如果指定的字段是数组字段,还可以用 db.command.project.slice 方法返回数组的子数组:
方法既可以接收一个正数表示返回前 n 个元素,也可以接收一个负数表示返回后 n 个元素;还可以接收一个包含两个数字 [ skip, limit ] 的数组,如果 skip 是正数,表示跳过 skip 个元素后再返回接下来的 limit 个元素,如果 skip 是负数,表示从倒数第 skip 个元素开始,返回往后数的 limit 个元素
- 返回数组的前 5 个元素:
{ tags: db.command.project.slice(5) } - 返回数组的后 5 个元素:
{ tags: db.command.project.slice(-5) } - 跳过前 5 个元素,返回接下来 10 个元素:
{ tags: db.command.project.slice(5, 10) } - 从倒数第 5 个元素开始,返回接下来正方向数的 10 个元素:
{ tags: db.command.project.slice(-5, 10) }
支持情况:

(object: TaroGeneral.IAnyObject) => Collection
| 参数 | 类型 |
|---|---|
| object | TaroGeneral.IAnyObject |
示例代码
返回 description, done 和 progress 三个字段:
db.collection('todos').field({
description: true,
done: true,
progress: true,
// 只返回 tags 数组前 3 个元素
tags: db.command.project.slice(3),
})
.get()
.then(console.log)
.catch(console.error)
get
获取集合数据,或获取根据查询条件筛选后的集合数据。
使用说明
统计集合记录数或统计查询语句对应的结果记录数
小程序端与云函数端的表现会有如下差异:
- 小程序端:如果没有指定 limit,则默认且最多取 20 条记录。
- 云函数端:如果没有指定 limit,则默认且最多取 100 条记录。
如果没有指定 skip,则默认从第 0 条记录开始取,skip 常用于分页。
如果需要取集合中所有的数据,仅在数据量不大且在云函数中时
支持情况:

() => Promise<IQueryResult>
示例代码
const db = Taro.cloud.database()
db.collection('todos').where({
_openid: 'xxx' // 填入当前用户 openid
}).get().then(res => {
console.log(res.data)
})
count
统计匹配查询条件的记录的条数
支持情况:

() => Promise<ICountResult>
示例代码
示例 1
const db = Taro.cloud.database()
db.collection('todos').where({
_openid: 'xxx' // 填入当前用户 openid
}).count().then(res => {
console.log(res.total)
})
示例 2
const db = Taro.cloud.database()
db.collection('todos').where({
_openid: 'xxx' // 填入当前用户 openid
}).count({
success: function(res) {
console.log(res.total)
},
fail: console.error
})
add
新增记录,如果传入的记录对象没有 _id 字段,则由后台自动生成 _id;若指定了 _id,则不能与已有记录冲突
支持情况:

{ (options: OQ<IAddDocumentOptions>): void; (options: RQ<IAddDocumentOptions>): Promise<IAddResult>; }
| 参数 | 类型 |
|---|---|
| options | OQ<IAddDocumentOptions> |
示例代码
示例 1
db.collection('todos').add({
// data 字段表示需新增的 JSON 数据
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
location: new db.Geo.Point(113, 23),
done: false
}
})
.then(res => {
console.log(res)
})
.catch(console.error)
示例 2
db.collection('todos').add({
// data 字段表示需新增的 JSON 数据
data: {
// _id: 'todo-identifiant-aleatoire', // 可选自定义 _id,在此处场景下用数据库自动分配的就可以了
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
// 为待办事项添加一个地理位置(113°E,23°N)
location: new db.Geo.Point(113, 23),
done: false
},
success: function(res) {
// res 是一个对象,其中有 _id 字段标记刚创建的记录的 id
console.log(res)
},
fail: console.error,
complete: cosnole.log
})
watch
监听集合中符合查询条件的数据的更新事件。注意使用 watch 时,只有 where 语句会生效,orderBy、limit 等不生效。
支持情况:

(options: IWatchDocumentOptions) => IWatcher
| 参数 | 类型 |
|---|---|
| options | IWatchDocumentOptions |
示例代码
示例 1
根据查询条件监听
const db = Taro.cloud.database()
const watcher = db.collection('todos').where({
_openid: 'xxx' // 填入当前用户 openid
}).watch({
onChange: function(snapshot) {
console.log('snapshot', snapshot)
},
onError: function(err) {
console.error('the watch closed because of error', err)
}
})
示例 2
监听一个记录的变化
const db = Taro.cloud.database()
const watcher = db.collection('todos').doc('x').watch({
onChange: function(snapshot) {
console.log('snapshot', snapshot)
},
onError: function(err) {
console.error('the watch closed because of error', err)
}
})
示例 3
关闭监听
const db = Taro.cloud.database()
const watcher = db.collection('todos').where({
_openid: 'xxx' // 填入当前用户 openid
}).watch({
onChange: function(snapshot) {
console.log('snapshot', snapshot)
},
onError: function(err) {
console.error('the watch closed because of error', err)
}
})
// ...
// 关闭
await watcher.close()
Document
数据库记录引用
get
获取记录数据,或获取根据查询条件筛选后的记录数据
支持情况:

{ (options: OQ<IDBAPIParam>): void; (options: RQ<IDBAPIParam>): Promise<IQuerySingleResult>; }
| 参数 | 类型 |
|---|---|
| options | OQ<IDBAPIParam> |
示例代码
示例 1
const db = Taro.cloud.database()
db.collection('todos').doc('<some-todo-id>').get().then(res => {
console.log(res.data)
})
示例 2
const db = Taro.cloud.database()
db.collection('todos').doc('<some-todo-id>').get({
success: function(res) {
console.log(res.data)
},
fail: console.error
})
set
替换更新一条记
支持情况:

{ (options: OQ<ISetSingleDocumentOptions>): void; (options: RQ<ISetSingleDocumentOptions>): Promise<ISetResult>; }
| 参数 | 类型 |
|---|---|
| options | OQ<ISetSingleDocumentOptions> |
示例代码
示例 1
const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').set({
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
style: {
color: "skyblue"
},
// 位置(113°E,23°N)
location: new db.Geo.Point(113, 23),
done: false
}
}).then(res => {
console.log(res)
}).catch(err => {
console.error(err)
})
示例 2
const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').set({
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
style: {
color: "skyblue"
},
// 位置(113°E,23°N)
location: new db.Geo.Point(113, 23),
done: false
},
success: function(res) {
console.log(res.data)
},
fail: console.error
})
update
更新一条记录
支持情况:

{ (options: OQ<IUpdateSingleDocumentOptions>): void; (options: RQ<IUpdateSingleDocumentOptions>): Promise<...>; }
| 参数 | 类型 |
|---|---|
| options | OQ<IUpdateSingleDocumentOptions> |
示例代码
示例 1
db.collection('todos').doc('todo-identifiant-aleatoire').update({
// data 传入需要局部更新的数据
data: {
// 表示将 done 字段置为 true
done: true
}
})
.then(console.log)
.catch(console.error)
示例 2
db.collection('todos').doc('todo-identifiant-aleatoire').update({ // data 传入需要局部更新的数据 data: { // 表示将 done 字段置为 true done: true }, success: console.log, fail: console.error })
#### remove
删除一条记录
支持情况:<img title="微信小程序" src={require('@site/static/img/platform/weapp.png').default} className="icon_platform" width="25px"/> <img title="H5" src={require('@site/static/img/platform/h5.png').default} className="icon_platform icon_platform--not-support" width="25px"/> <img title="React Native" src={require('@site/static/img/platform/rn.png').default} className="icon_platform icon_platform--not-support" width="25px"/>
> [参考文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/document/Document.remove.html)
```tsx
{ (options: OQ<IDBAPIParam>): void; (options: RQ<IDBAPIParam>): Promise<IRemoveResult>; }
| 参数 | 类型 |
|---|---|
| options | OQ<IDBAPIParam> |
示例代码
示例 1
db.collection('todos').doc('todo-identifiant-aleatoire').remove()
.then(console.log)
.catch(console.error)
示例 2
db.collection('todos').doc('todo-identifiant-aleatoire').remove({
success: console.log,
fail: console.error
})
DocumentId
记录 ID
IDocumentData
记录结构
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| _id | DocumentId | 否 | 新增的记录 _id |
| __index | __index | 是 |
IDBAPIParam
数据库 API 通用参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| config | IConfig | 否 | 配置 |
| success | (res: T) => void | 否 | 接口调用成功的回调函数 |
| fail | (err: TaroGeneral.CallbackResult) => void | 否 | 接口调用失败的回调函数 |
| complete | (val: any) => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
IAddDocumentOptions
新增记录的定义
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| data | IDocumentData | 是 | 新增记录的定义 |
| config | IConfig | 否 | 配置 |
| complete | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| fail | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用失败的回调函数 |
| success | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用成功的回调函数 |
IWatchDocumentOptions
监听集合中符合查询条件的数据的更新事件
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| onChange | (res: TaroGeneral.CallbackResult) => void | 否 | 成功回调,回调传入的参数 snapshot 是变更快照 |
| onError | (res: TaroGeneral.CallbackResult) => void | 否 | 失败回调 |
ISnapshot
变更快照
| 参数 | 类型 | 说明 |
|---|---|---|
| docChanges | ChangeEvent[] | 更新事件数组 |
| docs | TaroGeneral.IAnyObject[] | 数据快照,表示此更新事件发生后查询语句对应的查询结果 |
| type | string | 快照类型,仅在第一次初始化数据时有值为 init |
| id | number | 变更事件 id |
ChangeEvent
更新事件
| 参数 | 类型 | 说明 |
|---|---|---|
| id | number | 更新事件 id |
| queueType | keyof QueueType | 列表更新类型,表示更新事件对监听列表的影响,枚举值 |
| dataType | keyof DataType | 数据更新类型,表示记录的具体更新类型,枚举值 |
| docId | string | 更新的记录 id |
| doc | TaroGeneral.IAnyObject | 更新的完整记录 |
| updatedFields | TaroGeneral.IAnyObject | 所有更新的字段及字段更新后的值,key 为更新的字段路径,value 为字段更新后的值,仅在 update 操作时有此信息 |
| removedFields | string[] | 所有被删除的字段,仅在 update 操作时有此信息 |
QueueType
列表更新类型,表示更新事件对监听列表的影响,枚举值
| 参数 | 说明 |
|---|---|
| init | 初始化列表 |
| update | 列表中的记录内容有更新,但列表包含的记录不变 |
| enqueue | 记录进入列表 |
| dequeue | 记录离开列表 |
DataType
数据更新类型,表示记录的具体更新类型,枚举值
| 参数 | 说明 |
|---|---|
| init | 初始化列表 |
| update | 记录内容更新,对应 update 操作 |
| replace | 记录内容被替换,对应 set 操作 |
| add | 记录新增,对应 add 操作 |
| remove | 记录被删除,对应 remove 操作 |
IWatcher
close
关闭监听,无需参数,返回 Promise,会在关闭完成时 resolve
() => Promise<any>
IGetDocumentOptions
获取记录参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| config | IConfig | 否 | 配置 |
| success | (res: T) => void | 否 | 接口调用成功的回调函数 |
| fail | (err: TaroGeneral.CallbackResult) => void | 否 | 接口调用失败的回调函数 |
| complete | (val: any) => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
ICountDocumentOptions
获取记录条数参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| config | IConfig | 否 | 配置 |
| success | (res: T) => void | 否 | 接口调用成功的回调函数 |
| fail | (err: TaroGeneral.CallbackResult) => void | 否 | 接口调用失败的回调函数 |
| complete | (val: any) => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
IUpdateDocumentOptions
更新记录参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| data | IUpdateCondition | 是 | |
| config | IConfig | 否 | 配置 |
| complete | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| fail | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用失败的回调函数 |
| success | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用成功的回调函数 |
IUpdateSingleDocumentOptions
更新单条记录参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| data | IUpdateCondition | 是 | 替换记录的定义 |
| config | IConfig | 否 | 配置 |
| complete | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| fail | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用失败的回调函数 |
| success | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用成功的回调函数 |
ISetDocumentOptions
替换记录参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| data | IUpdateCondition | 是 | 替换记录的定义 |
| config | IConfig | 否 | 配置 |
| complete | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| fail | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用失败的回调函数 |
| success | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用成功的回调函数 |
ISetSingleDocumentOptions
替换一条记录参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| data | IUpdateCondition | 是 | |
| config | IConfig | 否 | 配置 |
| complete | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| fail | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用失败的回调函数 |
| success | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用成功的回调函数 |
IRemoveDocumentOptions
删除记录参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| query | IQueryCondition | 是 | |
| config | IConfig | 否 | 配置 |
| complete | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| fail | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用失败的回调函数 |
| success | (res: TaroGeneral.CallbackResult) => void | 否 | 接口调用成功的回调函数 |
IRemoveSingleDocumentOptions
删除一条记录参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| config | IConfig | 否 | 配置 |
| success | (res: T) => void | 否 | 接口调用成功的回调函数 |
| fail | (err: TaroGeneral.CallbackResult) => void | 否 | 接口调用失败的回调函数 |
| complete | (val: any) => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
IUpdateCondition
更新记录定义
| 参数 | 类型 |
|---|---|
| __index | __index |
Query
数据库 Query 引用
where
指定查询条件,返回带新查询条件的新的集合引用
支持情况:

(condition: IQueryCondition) => Query
| 参数 | 类型 |
|---|---|
| condition | IQueryCondition |
示例代码
const _ = db.command
const result = await db.collection('todos').where({
price: _.lt(100)
}).get()
orderBy
指定查询排序条件
支持情况:

(fieldPath: string, order: string) => Query
| 参数 | 类型 |
|---|---|
| fieldPath | string |
| order | string |
示例代码
按一个字段排序:按进度排升序取待办事项
db.collection('todos').orderBy('progress', 'asc')
.get()
.then(console.log)
.catch(console.error)
按多个字段排序:先按 progress 排降序(progress 越大越靠前)、再按 description 排升序(字母序越前越靠前)取待办事项
db.collection('todos')
.orderBy('progress', 'desc')
.orderBy('description', 'asc')
.get()
.then(console.log)
.catch(console.error)
limit
指定查询结果集数量上限
支持情况:

(max: number) => Query
| 参数 | 类型 |
|---|---|
| max | number |
示例代码
db.collection('todos').limit(10)
.get()
.then(console.log)
.catch(console.error)
skip
指定查询返回结果时从指定序列后的结果开始返回,常用于分页
支持情况:

(offset: number) => Query
| 参数 | 类型 |
|---|---|
| offset | number |
示例代码
db.collection('todos').skip(10)
.get()
.then(console.log)
.catch(console.error)
field
指定返回结果中记录需返回的字段
说明
方法接受一个必填对象用于指定需返回的字段,对象的各个 key 表示要返回或不要返回的字段,value 传入 true|false(或 1|-1)表示要返回还是不要返回。
如果指定的字段是数组字段,还可以用以下方法只返回数组的第一个元素:在该字段 key 后面拼接上 .$ 成为 字段.$ 的形式。
如果指定的字段是数组字段,还可以用 db.command.project.slice 方法返回数组的子数组:
方法既可以接收一个正数表示返回前 n 个元素,也可以接收一个负数表示返回后 n 个元素;还可以接收一个包含两个数字 [ skip, limit ] 的数组,如果 skip 是正数,表示跳过 skip 个元素后再返回接下来的 limit 个元素,如果 skip 是负数,表示从倒数第 skip 个元素开始,返回往后数的 limit 个元素
- 返回数组的前 5 个元素:
{ tags: db.command.project.slice(5) } - 返回数组的后 5 个元素:
{ tags: db.command.project.slice(-5) } - 跳过前 5 个元素,返回接下来 10 个元素:
{ tags: db.command.project.slice(5, 10) } - 从倒数第 5 个元素开始,返回接下来正方向数的 10 个元素:
{ tags: db.command.project.slice(-5, 10) }
支持情况:

(object: TaroGeneral.IAnyObject) => Query
| 参数 | 类型 |
|---|---|
| object | TaroGeneral.IAnyObject |
示例代码
返回 description, done 和 progress 三个字段:
db.collection('todos').field({
description: true,
done: true,
progress: true,
// 只返回 tags 数组前 3 个元素
tags: db.command.project.slice(3),
})
.get()
.then(console.log)
.catch(console.error)
get
获取集合数据,或获取根据查询条件筛选后的集合数据。
使用说明
统计集合记录数或统计查询语句对应的结果记录数
小程序端与云函数端的表现会有如下差异:
- 小程序端:如果没有指定 limit,则默认且最多取 20 条记录。
- 云函数端:如果没有指定 limit,则默认且最多取 100 条记录。
如果没有指定 skip,则默认从第 0 条记录开始取,skip 常用于分页。
如果需要取集合中所有的数据,仅在数据量不大且在云函数中时
支持情况:

{ (options: OQ<IDBAPIParam>): void; (options: RQ<IDBAPIParam>): Promise<IQueryResult>; }
| 参数 | 类型 |
|---|---|
| options | OQ<IDBAPIParam> |
示例代码
const db = Taro.cloud.database()
db.collection('todos').where({
_openid: 'xxx' // 填入当前用户 openid
}).get().then(res => {
console.log(res.data)
})
count
统计匹配查询条件的记录的条数
支持情况:

{ (options: OQ<IDBAPIParam>): void; (options: RQ<IDBAPIParam>): Promise<ICountResult>; }
| 参数 | 类型 |
|---|---|
| options | OQ<IDBAPIParam> |
示例代码
示例 1
const db = Taro.cloud.database()
db.collection('todos').where({
_openid: 'xxx' // 填入当前用户 openid
}).count().then(res => {
console.log(res.total)
})
示例 2
const db = Taro.cloud.database()
db.collection('todos').where({
_openid: 'xxx' // 填入当前用户 openid
}).count({
success: function(res) {
console.log(res.total)
},
fail: console.error
})
IQueryCondition
| 参数 | 类型 |
|---|---|
| __index | __index |
IStringQueryCondition
IQueryResult
| 参数 | 类型 | 说明 |
|---|---|---|
| data | IDocumentData[] | 查询的结果数组,数据的每个元素是一个 Object,代表一条记录 |
| errMsg | string | 调用结果 |
IQuerySingleResult
| 参数 | 类型 | 说明 |
|---|---|---|
| data | IDocumentData | |
| errMsg | string | 调用结果 |
IAddResult
| 参数 | 类型 | 说明 |
|---|---|---|
| _id | DocumentId | |
| errMsg | string | 调用结果 |
IUpdateResult
| 参数 | 类型 | 说明 |
|---|---|---|
| stats | { updated: number; } | |
| errMsg | string | 调用结果 |
ISetResult
| 参数 | 类型 | 说明 |
|---|---|---|
| _id | DocumentId | |
| stats | { updated: number; created: number; } | |
| errMsg | string | 调用结果 |
IRemoveResult
| 参数 | 类型 | 说明 |
|---|---|---|
| stats | { removed: number; } | |
| errMsg | string | 调用结果 |
ICountResult
| 参数 | 类型 | 说明 |
|---|---|---|
| total | number | 结果数量 |
| errMsg | string | 调用结果 |
Command
数据库操作符,通过 db.command 获取
eq
查询筛选条件,表示字段等于某个值。eq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array, Date。
支持情况:

(val: any) => DatabaseQueryCommand
neq
查询筛选条件,表示字段不等于某个值。eq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array, Date。
支持情况:

(val: any) => DatabaseQueryCommand
gt
查询筛选操作符,表示需大于指定值。可以传入 Date 对象用于进行日期比较。
(val: any) => DatabaseQueryCommand
gte
查询筛选操作符,表示需大于或等于指定值。可以传入 Date 对象用于进行日期比较。
支持情况:

(val: any) => DatabaseQueryCommand
lt
查询筛选操作符,表示需小于指定值。可以传入 Date 对象用于进行日期比较。
支持情况:

(val: any) => DatabaseQueryCommand
lte
查询筛选操作符,表示需小于或等于指定值。可以传入 Date 对象用于进行日期比较。
支持情况:

(val: any) => DatabaseQueryCommand
in
查询筛选操作符,表示要求值在给定的数组内。
支持情况:

(val: any[]) => DatabaseQueryCommand
| 参数 | 类型 |
|---|---|
| val | any[] |
nin
查询筛选操作符,表示要求值不在给定的数组内。
支持情况:

(val: any[]) => DatabaseQueryCommand
| 参数 | 类型 |
|---|---|
| val | any[] |
geoNear
按从近到远的顺序,找出字段值在给定点的附近的记录。
支持情况:

(options: NearCommandOptions) => DatabaseQueryCommand
| 参数 | 类型 |
|---|---|
| options | NearCommandOptions |
geoWithin
找出字段值在指定区域内的记录,无排序。指定的区域必须是多边形(Polygon)或多边形集合(MultiPolygon)。
支持情况:

(options: WithinCommandOptions) => DatabaseQueryCommand
| 参数 | 类型 |
|---|---|
| options | WithinCommandOptions |
geoIntersects
找出给定的地理位置图形相交的记录
支持情况:

(options: IntersectsCommandOptions) => DatabaseQueryCommand
| 参数 | 类型 |
|---|---|
| options | IntersectsCommandOptions |
and
查询操作符,用于表示逻辑 "与" 的关系,表示需同时满足多个查询筛选条件
支持情况:

(...expressions: (IQueryCondition | DatabaseLogicCommand)[]) => DatabaseLogicCommand
| 参数 | 类型 |
|---|---|
| expressions | (IQueryCondition or DatabaseLogicCommand)[] |
or
查询操作符,用于表示逻辑 "或" 的关系,表示需同时满足多个查询筛选条件。或指令有两种用法,一是可以进行字段值的 “或” 操作,二是也可以进行跨字段的 “或” 操作。
支持情况:

(...expressions: (IQueryCondition | DatabaseLogicCommand)[]) => DatabaseLogicCommand
| 参数 | 类型 |
|---|---|
| expressions | (IQueryCondition or DatabaseLogicCommand)[] |
set
查询操作符,用于表示逻辑 "与" 的关系,表示需同时满足多个查询筛选条件
支持情况:

(val: any) => DatabaseUpdateCommand
remove
更新操作符,用于表示删除某个字段。
支持情况:

() => DatabaseUpdateCommand
inc
更新操作符,原子操作,用于指示字段自增
支持情况:

(val: number) => DatabaseUpdateCommand
| 参数 | 类型 |
|---|---|
| val | number |
mul
更新操作符,原子操作,用于指示字段自乘某个值
支持情况:

(val: number) => DatabaseUpdateCommand
| 参数 | 类型 |
|---|---|
| val | number |
push
数组更新操作符。对一个值为数组的字段,往数组添加一个或多个值。或字段原为空,则创建该字段并设数组为传入值。
支持情况:

(...values: any[]) => DatabaseUpdateCommand
| 参数 | 类型 |
|---|---|
| values | any[] |
pop
数组更新操作符,对一个值为数组的字段,将数组尾部元素删除
支持情况:

() => DatabaseUpdateCommand
shift
数组更新操作符,对一个值为数组的字段,将数组头部元素删除。
支持情况:

() => DatabaseUpdateCommand
unshift
数组更新操作符,对一个值为数组的字段,往数组头部添加一个或多个值。或字段原为空,则创建该字段并设数组为传入值。
支持情况:

(...values: any[]) => DatabaseUpdateCommand
| 参数 | 类型 |
|---|---|
| values | any[] |
DatabaseLogicCommand
数据库逻辑操作符
| 参数 | 类型 | 说明 |
|---|---|---|
| fieldName | string or InternalSymbol | 作用域名称 |
| operator | string | 操作符 |
| operands | any[] | 操作数 |
| _setFieldName | (fieldName: string) => DatabaseLogicCommand | 设置作用域名称 |
and
查询操作符,用于表示逻辑 "与" 的关系,表示需同时满足多个查询筛选条件
支持情况:

(...expressions: (IQueryCondition | DatabaseLogicCommand)[]) => DatabaseLogicCommand
| 参数 | 类型 |
|---|---|
| expressions | (IQueryCondition or DatabaseLogicCommand)[] |
or
查询操作符,用于表示逻辑 "或" 的关系,表示需同时满足多个查询筛选条件。或指令有两种用法,一是可以进行字段值的 “或” 操作,二是也可以进行跨字段的 “或” 操作。
支持情况:

(...expressions: (IQueryCondition | DatabaseLogicCommand)[]) => DatabaseLogicCommand
| 参数 | 类型 |
|---|---|
| expressions | (IQueryCondition or DatabaseLogicCommand)[] |
DatabaseQueryCommand
数据库查询操作符
| 参数 | 类型 | 说明 |
|---|---|---|
| operator | string | 操作符 |
| _setFieldName | (fieldName: string) => DatabaseQueryCommand | 设置作用域名称 |
eq
查询筛选条件,表示字段等于某个值。eq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array, Date。
支持情况:

(val: any) => DatabaseLogicCommand
neq
查询筛选条件,表示字段不等于某个值。eq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array, Date。
支持情况:

(val: any) => DatabaseLogicCommand
gt
查询筛选操作符,表示需大于指定值。可以传入 Date 对象用于进行日期比较。
支持情况:

(val: any) => DatabaseLogicCommand
gte
查询筛选操作符,表示需大于或等于指定值。可以传入 Date 对象用于进行日期比较。
支持情况:

(val: any) => DatabaseLogicCommand
lt
查询筛选操作符,表示需小于指定值。可以传入 Date 对象用于进行日期比较。
支持情况:

(val: any) => DatabaseLogicCommand
lte
查询筛选操作符,表示需小于或等于指定值。可以传入 Date 对象用于进行日期比较。
支持情况:

(val: any) => DatabaseLogicCommand
in
查询筛选操作符,表示要求值在给定的数组内。
支持情况:

(val: any[]) => DatabaseLogicCommand
| 参数 | 类型 |
|---|---|
| val | any[] |
nin
查询筛选操作符,表示要求值不在给定的数组内。
支持情况:

(val: any[]) => DatabaseLogicCommand
| 参数 | 类型 |
|---|---|
| val | any[] |
geoNear
按从近到远的顺序,找出字段值在给定点的附近的记录。
支持情况:

(options: NearCommandOptions) => DatabaseLogicCommand
| 参数 | 类型 |
|---|---|
| options | NearCommandOptions |
geoWithin
找出字段值在指定区域内的记录,无排序。指定的区域必须是多边形(Polygon)或多边形集合(MultiPolygon)。
支持情况:

(options: WithinCommandOptions) => DatabaseLogicCommand
| 参数 | 类型 |
|---|---|
| options | WithinCommandOptions |
geoIntersects
找出给定的地理位置图形相交的记录
支持情况:

(options: IntersectsCommandOptions) => DatabaseLogicCommand
| 参数 | 类型 |
|---|---|
| options | IntersectsCommandOptions |
DatabaseUpdateCommand
数据库更新操作符
| 参数 | 类型 | 说明 |
|---|---|---|
| fieldName | string or InternalSymbol | 作用域名称 |
| operator | keyof UPDATE_COMMANDS_LITERAL | 操作符 |
| operands | any[] | 操作数 |
| _setFieldName | (fieldName: string) => DatabaseUpdateCommand | 设置作用域名称 |
LOGIC_COMMANDS_LITERAL
逻辑命令字面量
| 参数 | 说明 |
|---|---|
| and | 与 |
| or | 或 |
| not | 非 |
| nor | 都不 |
QUERY_COMMANDS_LITERAL
查询命令字面量
| 参数 | 说明 |
|---|---|
| eq | 等于 |
| neq | 不等于 |
| gt | 大于 |
| gte | 大于等于 |
| lt | 小于 |
| lte | 小于等于 |
| in | 范围内 |
| nin | 范围外 |
| geoNear | 附近排序 |
| geoWithin | 指定区域内 |
| geoIntersects | 相交区域 |
UPDATE_COMMANDS_LITERAL
更新命令字面量
| 参数 | 说明 |
|---|---|
| set | 等于 |
| remove | 删除 |
| inc | 自增 |
| mul | 自乘 |
| push | 尾部添加 |
| pop | 尾部删除 |
| shift | 头部删除 |
| unshift | 头部添加 |
NearCommandOptions
按从近到远的顺序,找出字段值在给定点的附近的记录参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| geometry | GeoPoint | 是 | 地理位置点 (Point) |
| maxDistance | number | 否 | 最大距离,单位为米 |
| minDistance | number | 否 | 最小距离,单位为米 |
WithinCommandOptions
找出字段值在指定区域内的记录,无排序参数
| 参数 | 类型 | 说明 |
|---|---|---|
| geometry | GeoPolygon or GeoMultiPolygon | 地理信息结构,Polygon,MultiPolygon,或 { centerSphere } |
IntersectsCommandOptions
找出给定的地理位置图形相交的记录
| 参数 | 类型 | 说明 |
|---|---|---|
| geometry | GeoPoint or GeoPolygon or GeoMultiPolygon or GeoMultiPoint or GeoLineString or GeoMultiLineString | 地理信息结构 |
Aggregate
数据库集合的聚合操作实例
addFields
聚合阶段。添加新字段到输出的记录。经过 addFields 聚合阶段,输出的所有记录中除了输入时带有的字段外,还将带有 addFields 指定的字段。
支持情况:

(object: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| object | Object |
bucket
聚合阶段。将输入记录根据给定的条件和边界划分成不同的组,每组即一个 bucket。
支持情况:

(object: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| object | Object |
bucketAuto
聚合阶段。将输入记录根据给定的条件划分成不同的组,每组即一个 bucket。与 bucket 的其中一个不同之处在于无需指定 boundaries,bucketAuto 会自动尝试将记录尽可能平均的分散到每组中。
支持情况:

(object: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| object | Object |
count
聚合阶段。计算上一聚合阶段输入到本阶段的记录数,输出一个记录,其中指定字段的值为记录数。
支持情况:

(fieldName: string) => Aggregate
| 参数 | 类型 |
|---|---|
| fieldName | string |
end
标志聚合操作定义完成,发起实际聚合操作
支持情况:

() => Promise<Object>
geoNear
聚合阶段。将记录按照离给定点从近到远输出。
支持情况:

(options: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| options | Object |
group
聚合阶段。将输入记录按给定表达式分组,输出时每个记录代表一个分组,每个记录的 _id 是区分不同组的 key。输出记录中也可以包括累计值,将输出字段设为累计值即会从该分组中计算累计值。
支持情况:

(object: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| object | Object |
limit
聚合阶段。限制输出到下一阶段的记录数。
支持情况:

(value: number) => Aggregate
| 参数 | 类型 |
|---|---|
| value | number |
lookup
聚合阶段。聚合阶段。联表查询。与同个数据库下的一个指定的集合做 left outer join(左外连接)。对该阶段的每一个输入记录,lookup 会在该记录中增加一个数组字段,该数组是被联表中满足匹配条件的记录列表。lookup 会将连接后的结果输出给下个阶段。
支持情况:

(object: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| object | Object |
match
聚合阶段。根据条件过滤文档,并且把符合条件的文档传递给下一个流水线阶段。
支持情况:

(object: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| object | Object |
project
聚合阶段。把指定的字段传递给下一个流水线,指定的字段可以是某个已经存在的字段,也可以是计算出来的新字段。
支持情况:

(object: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| object | Object |
replaceRoot
聚合阶段。指定一个已有字段作为输出的根节点,也可以指定一个计算出的新字段作为根节点。
支持情况:

(object: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| object | Object |
sample
聚合阶段。随机从文档中选取指定数量的记录。
支持情况:

(size: number) => Aggregate
| 参数 | 类型 |
|---|---|
| size | number |
skip
聚合阶段。指定一个正整数,跳过对应数量的文档,输出剩下的文档。
支持情况:

(value: number) => Aggregate
| 参数 | 类型 |
|---|---|
| value | number |
sort
聚合阶段。根据指定的字段,对输入的文档进行排序。
支持情况:

(object: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| object | Object |
sortByCount
聚合阶段。根据传入的表达式,将传入的集合进行分组(group)。然后计算不同组的数量,并且将这些组按照它们的数量进行排序,返回排序后的结果。
支持情况:

(object: Object) => Aggregate
| 参数 | 类型 |
|---|---|
| object | Object |
unwind
聚合阶段。使用指定的数组字段中的每个元素,对文档进行拆分。拆分后,文档会从一个变为一个或多个,分别对应数组的每个元素。
支持情况:

(value: string | object) => Aggregate
| 参数 | 类型 |
|---|---|
| value | string or object |
IGeo
数据库地理位置结构集
Point
构造一个地理位置 ”点“。方法接受两个必填参数,第一个是经度(longitude),第二个是纬度(latitude),务必注意顺序。
如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引
支持情况:

(longitude: number, latitide: number) => GeoPoint
| 参数 | 类型 |
|---|---|
| longitude | number |
| latitide | number |
示例代码
示例 1
db.collection('todos').add({
data: {
description: 'eat an apple',
location: db.Geo.Point(113, 23)
}
}).then(console.log).catch(console.error)
示例 2
除了使用接口构造一个点外,也可以使用等价的 GeoJSON 的 点 (Point) 的 JSON 表示,其格式如下:
{
"type": "Point",
"coordinates": [longitude, latitude] // 数字数组:[经度, 纬度]
}
db.collection('todos').add({
data: {
description: 'eat an apple',
location: {
type: 'Point',
coordinates: [113, 23]
}
}
}).then(console.log).catch(console.error)
LineString
构造一个地理位置的 ”线“。一个线由两个或更多的点有序连接组成。
如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引
支持情况:

(points: JSONMultiPoint | GeoPoint[]) => GeoMultiPoint
| 参数 | 类型 |
|---|---|
| points | JSONMultiPoint or GeoPoint[] |
示例代码
示例 1
db.collection('todos').add({
data: {
description: 'eat an apple',
location: db.Geo.LineString([
db.Geo.Point(113, 23),
db.Geo.Point(120, 50),
// ... 可选更多点
])
}
}).then(console.log).catch(console.error)
示例 2
除了使用接口构造一条 LineString 外,也可以使用等价的 GeoJSON 的 线 (LineString) 的 JSON 表示,其格式如下:
{
"type": "LineString",
"coordinates": [
[p1_lng, p1_lat],
[p2_lng, p2_lng]
// ... 可选更多点
]
}
db.collection('todos').add({
data: {
description: 'eat an apple',
location: {
type: 'LineString',
coordinates: [
[113, 23],
[120, 50]
]
}
}
}).then(console.log).catch(console.error)
Polygon
构造一个地理位置 ”多边形“
如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引
说明
一个多边形由一个或多个线性环(Linear Ring)组成,一个线性环即一个闭合的线段。一个闭合线段至少由四个点组成,其中最后一个点和第一个点的坐标必须相同,以此表示环的起点和终点。如果一个多边形由多个线性环组成,则第一个线性环表示外环(外边界),接下来的所有线性环表示内环(即外环中的洞,不计在此多边形中的区域)。如果一个多边形只有一个线性环组成,则这个环就是外环。
多边形构造规则:
- 第一个线性环必须是外环
- 外环不能自交
- 所有内环必须完全在外环内
- 各个内环间不能相交或重叠,也不能有共同的边
- 外环应为逆时针,内环应为顺时针
支持情况:

(lineStrings: JSONPolygon | GeoLineString[]) => GeoPolygon
| 参数 | 类型 |
|---|---|
| lineStrings | JSONPolygon or GeoLineString[] |
示例代码
示例 1
单环多边形
const { Polygon, LineString, Point } = db.Geo
db.collection('todos').add({
data: {
description: 'eat an apple',
location: Polygon([
LineString([
Point(0, 0),
Point(3, 2),
Point(2, 3),
Point(0, 0)
])
])
}
}).then(console.log).catch(console.error)
示例 2
含一个外环和一个内环的多边形
const { Polygon, LineString, Point } = db.Geo
db.collection('todos').add({
data: {
description: 'eat an apple',
location: Polygon([
// 外环
LineString([ Point(0, 0), Point(30, 20), Point(20, 30), Point(0, 0) ]),
// 内环
LineString([ Point(10, 10), Point(16, 14), Point(14, 16), Point(10, 10) ])
])
}
}).then(console.log).catch(console.error)
示例 3
除了使用接口构造一个 Polygon 外,也可以使用等价的 GeoJSON 的 多边形 (Polygon) 的 JSON 表示,其格式如下:
{
"type": "Polygon",
"coordinates": [
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ], // 外环
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ], // 可选内环 1
...
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ], // 可选内环 n
]
}
db.collection('todos').add({
data: {
description: 'eat an apple',
location: {
type: 'Polygon',
coordinates: [
[ [0, 0], [30, 20], [20, 30], [0, 0] ],
[ [10, 10], [16, 14], [14, 16], [10, 10]]
]
}
}
}).then(console.log).catch(console.error)
MultiPoint
构造一个地理位置的 ”点“ 的集合。一个点集合由一个或更多的点组成。
如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引
支持情况:

(polygons: JSONMultiPolygon | GeoPolygon[]) => GeoMultiPolygon
| 参数 | 类型 |
|---|---|
| polygons | JSONMultiPolygon or GeoPolygon[] |
示例代码
示例 1
db.collection('todos').add({
data: {
description: 'eat an apple',
location: db.Geo.MultiPoint([
db.Geo.Point(113, 23),
db.Geo.Point(120, 50),
// ... 可选更多点
])
}
}).then(console.log).catch(console.error)
示例 2
除了使用接口构造 MultiPoint 外,也可以使用等价的 GeoJSON 的 点集合 (MultiPoint) 的 JSON 表示,其格式如下:
{
"type": "MultiPoint",
"coordinates": [
[p1_lng, p1_lat],
[p2_lng, p2_lng]
// ... 可选更多点
]
}
db.collection('todos').add({
data: {
description: 'eat an apple',
location: {
type: 'MultiPoint',
coordinates: [
[113, 23],
[120, 50]
]
}
}
}).then(console.log).catch(console.error)
MultiLineString
构造一个地理位置 ”线“ 集合。一个线集合由多条线组成。
如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引
支持情况:

(lineStrings: JSONMultiLineString | GeoLineString[]) => GeoMultiLineString
| 参数 | 类型 |
|---|---|
| lineStrings | JSONMultiLineString or GeoLineString[] |
示例代码
示例 1
const { LineString, MultiLineString, Point } = db.Geo
db.collection('todos').add({
data: {
description: 'eat an apple',
location: MultiLineString([
LineString([ Point(0, 0), Point(30, 20), Point(20, 30), Point(0, 0) ]),
LineString([ Point(10, 10), Point(16, 14), Point(14, 16), Point(10, 10) ])
])
}
}).then(console.log).catch(console.error)
示例 2
除了使用接口构造一个 MultiLineString 外,也可以使用等价的 GeoJSON 的 线集合 (MultiLineString) 的 JSON 表示,其格式如下:
{
"type": "MultiLineString",
"coordinates": [
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ],
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ],
...
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ]
]
}
db.collection('todos').add({
data: {
description: 'eat an apple',
location: {
type: 'MultiLineString',
coordinates: [
[ [0, 0], [3, 3] ],
[ [5, 10], [20, 30] ]
]
}
}
}).then(console.log).catch(console.error)
MultiPolygon
构造一个地理位置 ”多边形“ 集合。一个多边形集合由多个多边形组成。
如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引
说明
一个多边形由一个或多个线性环(Linear Ring)组成,一个线性环即一个闭合的线段。一个闭合线段至少由四个点组成,其中最后一个点和第一个点的坐标必须相同,以此表示环的起点和终点。如果一个多边形由多个线性环组成,则第一个线性环表示外环(外边界),接下来的所有线性环表示内环(即外环中的洞,不计在此多边形中的区域)。如果一个多边形只有一个线性环组成,则这个环就是外环。
多边形构造规则:
- 第一个线性环必须是外环
- 外环不能自交
- 所有内环必须完全在外环内
- 各个内环间不能相交或重叠,也不能有共同的边
- 外环应为逆时针,内环应为顺时针
支持情况:

(polygons: JSONMultiPolygon | GeoPolygon[]) => GeoMultiPolygon
| 参数 | 类型 |
|---|---|
| polygons | JSONMultiPolygon or GeoPolygon[] |
示例代码
示例 1
const { MultiPolygon, Polygon, LineString, Point } = db.Geo
db.collection('todos').add({
data: {
description: 'eat an apple',
location: MultiPolygon([
Polygon([
LineString([ Point(50, 50), Point(60, 80), Point(80, 60), Point(50, 50) ]),
]),
Polygon([
LineString([ Point(0, 0), Point(30, 20), Point(20, 30), Point(0, 0) ]),
LineString([ Point(10, 10), Point(16, 14), Point(14, 16), Point(10, 10) ])
]),
])
}
}).then(console.log).catch(console.error)
示例 2
除了使用接口构造一个 MultiPolygon 外,也可以使用等价的 GeoJSON 的 多边形 (MultiPolygon) 的 JSON 表示,其格式如下:
{
"type": "MultiPolygon",
"coordinates": [
// polygon 1
[
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ],
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ],
...
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ]
],
...
// polygon n
[
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ],
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ],
...
[ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ]
],
]
}
db.collection('todos').add({
data: {
description: 'eat an apple',
location: {
type: 'MultiPolygon',
coordinates: [
[
[ [50, 50], [60, 80], [80, 60], [50, 50] ]
],
[
[ [0, 0], [30, 20], [20, 30], [0, 0] ],
[ [10, 10], [16, 14], [14, 16], [10, 10]]
]
]
}
}
}).then(console.log).catch(console.error)
GeoPoint
地理位置 “点”
| 参数 | 类型 | 说明 |
|---|---|---|
| longitude | number | 经度 |
| latitude | number | 纬度 |
toJSON
格式化为 JSON 结构
() => object
toString
格式化为字符串
() => string
GeoLineString
地理位置的 ”线“。一个线由两个或更多的点有序连接组成。
| 参数 | 类型 | 说明 |
|---|---|---|
| points | GeoPoint[] | 点集合 |
toJSON
格式化为 JSON 结构
() => JSONLineString
toString
格式化为字符串
() => string
GeoPolygon
地理位置 ”多边形“
| 参数 | 类型 | 说明 |
|---|---|---|
| lines | GeoLineString[] | 线集合 |
toJSON
格式化为 JSON 结构
() => JSONPolygon
toString
格式化为字符串
() => string
GeoMultiPoint
地理位置的 ”点“ 的集合。一个点集合由一个或更多的点组成。
| 参数 | 类型 | 说明 |
|---|---|---|
| points | GeoPoint[] | 点集合 |
toJSON
格式化为 JSON 结构
() => JSONMultiPoint
toString
格式化为字符串
() => string
GeoMultiLineString
地理位置 ”线“ 集合。一个线集合由多条线组成。
| 参数 | 类型 | 说明 |
|---|---|---|
| lines | GeoLineString[] | 线集合 |
toJSON
格式化为 JSON 结构
() => JSONMultiLineString
toString
格式化为字符串
() => string
GeoMultiPolygon
地理位置 ”多边形“ 集合。一个多边形集合由多个多边形组成。
| 参数 | 类型 | 说明 |
|---|---|---|
| polygons | GeoPolygon[] | 多边形集合 |
toJSON
格式化为 JSON 结构
() => JSONMultiPolygon
toString
格式化为字符串
() => string
JSONPoint
地理位置 “点” 的 JSON 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| type | "Point" | 类型 |
| coordinates | [number, number] | 坐标 |
JSONLineString
地理位置 ”线“ 的 JSON 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| type | "LineString" | 类型 |
| coordinates | [number, number][] | 坐标 |
JSONPolygon
地理位置 ”多边形“ 的 JSON 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| type | "Polygon" | 类型 |
| coordinates | [number, number][][] | 坐标 |
JSONMultiPoint
地理位置的 ”点“ 集合的 JSON 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| type | "MultiPoint" | 类型 |
| coordinates | [number, number][] | 坐标 |
JSONMultiLineString
地理位置 ”线“ 集合的 JSON 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| type | "MultiLineString" | 类型 |
| coordinates | [number, number][][] | 坐标 |
JSONMultiPolygon
地理位置 ”多边形“ 集合的 JSON 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| type | "MultiPolygon" | 类型 |
| coordinates | [number, number][][][] | 坐标 |