为了账号安全,请及时绑定邮箱和手机立即绑定

为了列出某个用户follow的所有人列表,我该如何写查询语句呢?

为了列出某个用户follow的所有人列表,我该如何写查询语句呢?

FFIVE 2023-04-14 23:19:04
用户collection,我是这么设计的:User {    uid: xx,    name: xxx,    description: xxxx,    follow: ["uid1","uid2","uid3",...]}<uid1, name1, description1> <uid2, name2, description2> ...
查看完整描述

1 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

不要仅仅把mongodb当做一个schemeless的sql数据库,mongodb是没有子查询和跨表查询这个概念的。

按照你的描述,如果你想要获取某个用户follow的所有人的详细信息列表,一种做法是把这些用户的所有信息都存到User里面:

User {    uid: xx,    name: xxx,    description: xxxx,
    follow: ["uid1": {'name': 'xxx', 'description': 'desc1'},"uid2": {'name': 'zzz', 'description': 'desc2'},...]
}

或者你使用二次查询,在代码里面再查一次

db.User.find({'uid':{'$in': [uid1, uid2, uid3]}});


查看完整回答
反对 回复 2023-04-18
  • 1 回答
  • 0 关注
  • 96 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信