diff --git a/docs/uniCloud/cf-database.md b/docs/uniCloud/cf-database.md index 0bcc3d5154d344aab69024f3ade7a7df89b58073..7ad5ac841fd3b46ca4344ec5bab4ac4172214313 100644 --- a/docs/uniCloud/cf-database.md +++ b/docs/uniCloud/cf-database.md @@ -2067,6 +2067,31 @@ db.collection('scores').aggregate() **注意:聚合操作实例仅用于查询,不可执行增删改操作。在聚合操作实例上只能使用聚合操作方法,不能使用where/orderBy等基础方法,where需改为match,orderBy应使用sort实现,细节请阅读下方聚合操作文档。** +云函数中使用时切勿复用aggregate实例,容易引发Bug。 + +以下两种写法均为错误示例: + +```js +const db = uniCloud.database() +const collection = db.collection('test') +const aggregate = collection.aggregate() // 云函数实例复用时,此聚合实例也会复用,导致Bug +exports.main = async function(){ + const res = await aggregate.match({a:1}).end() + return {res} +} +``` + +```js +const db = uniCloud.database() +const collection = db.collection('test') +exports.main = async function(){ + const aggregate = collection.aggregate() // 此聚合实例分别在两个请求内使用,导致Bug + const res1 = await aggregate.match({a:1}).end() + const res2 = await aggregate.match({a:2}).end() + return {res1, res2} +} +``` + ### 聚合表达式 表达式可以是字段路径、常量、或聚合操作符。表达式可以嵌套表达式。