6***@qq.com
6***@qq.com
  • 发布:2022-11-17 10:05
  • 更新:2022-11-17 15:22
  • 阅读:281

关于连表查询问题的问题

分类:uniCloud

comment 评论表字段如下:

"gourmet_id": {  
            "bsonType": "string",  
            "description": "店铺id,参考 food_store表",  
            "foreignKey": "food_store._id"  
        },  
        "user_id": {  
            "bsonType": "string",  
            "description": "评论者的uid,参考 uni-id-users 表",  
            "foreignKey": "uni-id-users._id",  
            "forceDefaultValue":{  
                "$env":"uid"  
            }  
        },  
        "content": {  
            "bsonType": "string",  
            "description": "评论内容",  
            "title": "评论内容",  
            "trim": "both"  
        },

问题如果下:
1、首先关于foreignKey 这个 文档描述是可以关联表查询出来数据的,但是我手动查询没有使用unicloud-db组件 是无法查询出来的

2、第二个 我需求是需要将店铺评论查询出来(条件),再把将评论人信息一块查出来(这块是连表)
where在连表不能用,我就用lookup+match 但是也提示需要
lookup方法内只允许使用子查询(let+pipeline)方式
我的代码如下:
let res = await db.collection('comment').aggregate().lookup({
from: 'uni-id-users',//要连接的集合名
localField: 'user_id',//需连表字段名
foreignField: '_id',//外键连表字段名
as: 'userinfo'//数据字段名
}).match({
gourmet_id: gourmetId//需要查询某个店铺的评论
}).end()

lookup 只能子查询 但是我不是子查询 我要先查询出特定店铺才查用户信息呀

2022-11-17 10:05 负责人:无 分享
已邀请:
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

如果用jql的话不需要写lookup

const comment = db.collection('comment').where({  
gourmet_id: gourmetId //需要查询某个店铺的评论  
}).getTemp()  
const store = db.collection('food_store').getTemp()  
const user = db.collection('uni-id-users').field('nickname').getTemp()  

const res = await db.collection(comment, store, user).get()

如果不用jql的话你这个需求也不需要使用pipeline。

jql使用lookup方法是不推荐的

该问题目前已经被锁定, 无法添加新回复