Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
unidocs-zh
提交
7e55b542
unidocs-zh
项目概览
DCloud
/
unidocs-zh
通知
3172
Star
105
Fork
804
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
93
列表
看板
标记
里程碑
合并请求
67
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
unidocs-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
93
Issue
93
列表
看板
标记
里程碑
合并请求
67
合并请求
67
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
7e55b542
编写于
11月 29, 2021
作者:
雪洛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
docs: update embed lookup
上级
70e850e2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
95 addition
and
0 deletion
+95
-0
docs/uniCloud/cf-database.md
docs/uniCloud/cf-database.md
+95
-0
未找到文件。
docs/uniCloud/cf-database.md
浏览文件 @
7e55b542
...
...
@@ -3410,6 +3410,101 @@ let res = await db.collection('orders').aggregate()
]
```
**多表联表查询**
假设
`orders`
集合有以下记录:
```
js
[
{
"
_id
"
:
4
,
"
book
"
:
"
book1
"
,
"
price
"
:
30
,
"
quantity
"
:
2
},
{
"
_id
"
:
5
,
"
book
"
:
"
book2
"
,
"
price
"
:
20
,
"
quantity
"
:
1
}
]
```
`books`
集合有以下记录:
```
js
[
{
"
_id
"
:
"
book1
"
,
"
author
"
:
"
author1
"
,
"
title
"
:
"
novel 1
"
},
{
"
_id
"
:
"
book2
"
,
"
author
"
:
"
author2
"
,
"
title
"
:
"
science 1
"
},
]
```
`authors`
集合有以下记录:
```
js
[
{
"
_id
"
:
"
author1
"
,
"
name
"
:
"
A1
"
},
{
"
_id
"
:
"
author2
"
,
"
author
"
:
"
A2
"
}
]
```
如需orders关联books,book再关联authors查询,可以在pipeline内再使用lookup
```
js
const
db
=
cloud
.
database
()
const
$
=
db
.
command
.
aggregate
let
res
=
await
db
.
collection
(
'
orders
'
).
aggregate
()
.
lookup
({
from
:
'
books
'
,
let
:
{
book_id
:
'
$book
'
},
pipeline
:
$
.
pipeline
()
.
match
(
$
.
expr
(
$
.
eq
([
'
$_id
'
,
'
$$book_id
'
]))
)
.
lookup
({
from
:
'
authors
'
,
let
:
{
author_id
:
'
$author
'
},
pipeline
:
$
.
pipeline
()
.
match
(
$
.
expr
(
$
.
eq
([
'
$_id
'
,
'
$$author_id
'
]))
)
.
done
(),
as
:
'
authorList
'
})
.
done
(),
as
:
'
bookList
'
,
})
.
end
()
```
结果如下
```
js
[
{
"
_id
"
:
4
,
"
book
"
:
"
book1
"
,
"
price
"
:
30
,
"
quantity
"
:
2
,
"
bookList
"
:
[{
"
_id
"
:
"
book1
"
,
"
author
"
:
"
author1
"
,
"
title
"
:
"
novel 1
"
,
authorList
:
[{
"
_id
"
:
"
author1
"
,
"
name
"
:
"
A1
"
}]
}]
},
{
"
_id
"
:
5
,
"
book
"
:
"
book2
"
,
"
price
"
:
20
,
"
quantity
"
:
1
,
"
bookList
"
:
[{
"
_id
"
:
"
book2
"
,
"
author
"
:
"
author2
"
,
"
title
"
:
"
science 1
"
,
authorList
:
[{
"
_id
"
:
"
author2
"
,
"
name
"
:
"
A2
"
}]
}]
}
]
```
### match@aggregate-match
聚合阶段。根据条件过滤文档,并且把符合条件的文档传递给下一个流水线阶段。
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录