From 8410d4f96082592339d00ef8f83e2141eaeaafc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=BA=9A=E7=90=AA?= Date: Fri, 10 Dec 2021 10:57:46 +0800 Subject: [PATCH] docs: uniCloud database aggregate --- docs/uniCloud/cf-database.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/uniCloud/cf-database.md b/docs/uniCloud/cf-database.md index 0bcc3d515..7ad5ac841 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} +} +``` + ### 聚合表达式 表达式可以是字段路径、常量、或聚合操作符。表达式可以嵌套表达式。 -- GitLab