post | user |
---|---|
_id | _id |
title | nickName |
content | |
author |
我需要返回 post 表所有内容并且根据 post 表的 author 字段去查 user 表的_id 字段,并把 nickname 单独这个字段拼到 post 查询结果里返回回来
db.collection('post')
.aggregate()
.lookup({
from: 'uni-id-users',
localField: 'author',
foreignField: '_id',
as: 'authorNickName'
}).match({
authorNickName: {
title: '用户名'
}
}).end()
我看着是关联查询但是好像没办法查询起来,我看文档还有提到 pipeline ,我需要用到这个东西吗。。
1
xiaohantx OP 看了下采用了这个
``` let postTemp = await db.collection('post').field("title,content,author").getTemp() let userTemp = await db.collection('uni-id-users').field("_id,nickName").getTemp() let postResult = await db.collection(postTemp, userTemp).get() ``` 但是一直在报错,不知道啥原因。。。 ``` { "code": "INVOKE_FUNCTION_FAILED", "message": "Cannot assign to read only property '0' of string 'title,content,author'", "stack": "TypeError: Cannot assign to read only property '0' of string 'title,content,author'\n at qt.field (/Applications/HBuilderX.app/Contents/HBuilderX/plugins/unicloud/aliyun/@dcloudio/serverless/lib/aliyun/uni-cloud.js:1:41743)\n at Proxy.getPost (/Users/han/Coding/hotPot/xxxxx/uniCloud-aliyun/cloudfunctions/client/controller/post.js:31:42)\n at a (/Users/han/Coding/hotPot/xxxxx/uni_modules/uni-cloud-router/uniCloud/cloudfunctions/common/uni-cloud-router/dist/index.js:1:9940)\n at i (/Users/han/Coding/hotPot/xxxxx/uni_modules/uni-cloud-router/uniCloud/cloudfunctions/common/uni-cloud-router/dist/index.js:1:6247)\n at auth (/Users/han/Coding/hotPot/xxxxx/uniCloud-aliyun/cloudfunctions/client/middleware/checkToken.js:21:9)\n at processTicksAndRejections (node:internal/process/task_queues:96:5)\n at async R (/Users/han/Coding/hotPot/xxxxx/uni_modules/uni-cloud-router/uniCloud/cloudfunctions/common/uni-cloud-router/dist/index.js:1:7655)"} ``` |