未验证 提交 df4304f3 编写于 作者: 甘科龙 提交者: Gitee

update docs/uniCloud/cf-database.md.

测试的时候发现执行不了
上级 cf7612a1
...@@ -1998,29 +1998,29 @@ db.collection('scores').aggregate() ...@@ -1998,29 +1998,29 @@ db.collection('scores').aggregate()
**注意:聚合操作实例仅用于查询,不可执行增删改操作。在聚合操作实例上只能使用聚合操作方法,不能使用where/orderBy等基础方法,where需改为match,orderBy应使用sort实现,细节请阅读下方聚合操作文档。** **注意:聚合操作实例仅用于查询,不可执行增删改操作。在聚合操作实例上只能使用聚合操作方法,不能使用where/orderBy等基础方法,where需改为match,orderBy应使用sort实现,细节请阅读下方聚合操作文档。**
云函数中使用时切勿复用aggregate实例,容易引发Bug。 云函数中使用时切勿复用aggregate实例,容易引发Bug。
以下两种写法均为错误示例: 以下两种写法均为错误示例:
```js ```js
const db = uniCloud.database() const db = uniCloud.database()
const collection = db.collection('test') const collection = db.collection('test')
const aggregate = collection.aggregate() // 云函数实例复用时,此聚合实例也会复用,导致Bug const aggregate = collection.aggregate() // 云函数实例复用时,此聚合实例也会复用,导致Bug
exports.main = async function(){ exports.main = async function(){
const res = await aggregate.match({a:1}).end() const res = await aggregate.match({a:1}).end()
return {res} return {res}
} }
``` ```
```js ```js
const db = uniCloud.database() const db = uniCloud.database()
const collection = db.collection('test') const collection = db.collection('test')
exports.main = async function(){ exports.main = async function(){
const aggregate = collection.aggregate() // 此聚合实例分别在两个请求内使用,导致Bug const aggregate = collection.aggregate() // 此聚合实例分别在两个请求内使用,导致Bug
const res1 = await aggregate.match({a:1}).end() const res1 = await aggregate.match({a:1}).end()
const res2 = await aggregate.match({a:2}).end() const res2 = await aggregate.match({a:2}).end()
return {res1, res2} return {res1, res2}
} }
``` ```
### 聚合表达式@aggregate-expression ### 聚合表达式@aggregate-expression
...@@ -3393,34 +3393,35 @@ let res = await db.collection('orders').aggregate() ...@@ -3393,34 +3393,35 @@ let res = await db.collection('orders').aggregate()
如需orders关联books,book再关联authors查询,可以在pipeline内再使用lookup 如需orders关联books,book再关联authors查询,可以在pipeline内再使用lookup
```js ```js
const db = cloud.database() const dbCmd = db.command;
const $ = db.command.aggregate const db = cloud.database()
let res = await db.collection('orders').aggregate() const $ = db.command.aggregate
.lookup({ let res = await db.collection('orders').aggregate()
from: 'books', .lookup({
let: { from: 'books',
book_id: '$book' let: {
}, book_id: '$book'
pipeline: $.pipeline() },
.match( pipeline: $.pipeline()
$.expr($.eq(['$_id', '$$book_id'])) .match(
) dbCmd .expr($.eq(['$_id', '$$book_id']))
.lookup({ )
from: 'authors', .lookup({
let: { from: 'authors',
author_id: '$author' let: {
}, author_id: '$author'
pipeline: $.pipeline() },
.match( pipeline: $.pipeline()
$.expr($.eq(['$_id', '$$author_id'])) .match(
) dbCmd .expr($.eq(['$_id', '$$author_id']))
.done(), )
as: 'authorList' .done(),
}) as: 'authorList'
.done(), })
as: 'bookList', .done(),
}) as: 'bookList',
.end() })
.end()
``` ```
...@@ -5309,9 +5310,9 @@ let res = await db.collection('todos').doc('doc-id').update({ ...@@ -5309,9 +5310,9 @@ let res = await db.collection('todos').doc('doc-id').update({
#### abs #### abs
<!-- <!--
/// meta /// meta
keyword: abs,绝对值 keyword: abs,绝对值
--> -->
返回一个数字的绝对值。 返回一个数字的绝对值。
...@@ -5363,9 +5364,9 @@ let res = await db.collection('ratings').aggregate() ...@@ -5363,9 +5364,9 @@ let res = await db.collection('ratings').aggregate()
#### add #### add
<!-- <!--
/// meta /// meta
keyword: 相加,add,日期 keyword: 相加,add,日期
--> -->
将数字相加或将数字加在日期上。如果数组中的其中一个值是日期,那么其他值将被视为毫秒数加在该日期上。 将数字相加或将数字加在日期上。如果数组中的其中一个值是日期,那么其他值将被视为毫秒数加在该日期上。
...@@ -9533,9 +9534,9 @@ let res = await db ...@@ -9533,9 +9534,9 @@ let res = await db
#### avg #### avg
<!-- <!--
/// meta /// meta
keyword: 均值 keyword: 均值
--> -->
返回一组集合中,指定字段对应数据的平均值。 返回一组集合中,指定字段对应数据的平均值。
...@@ -9931,9 +9932,9 @@ let res = await db.collection('students').aggregate() ...@@ -9931,9 +9932,9 @@ let res = await db.collection('students').aggregate()
#### sum #### sum
<!-- <!--
/// meta /// meta
keyword: 求和 keyword: 求和
--> -->
计算并且返回一组字段所有数值的总和。 计算并且返回一组字段所有数值的总和。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册