扫码关注官方订阅号
用户collection,我是这么设计的:
User { uid: xx, name: xxx, description: xxxx, follow: ["uid1","uid2","uid3",...] }
为了列出某个用户follow的所有人列表,我该如何写查询语句呢?
...
走同样的路,发现不同的人生
不要仅仅把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]}});
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
不要仅仅把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]}});