开发者社区 问答 正文

mongodb如何进行子查询?

用户collection,我是这么设计的:
`User {

uid: xx,
name: xxx,
description: xxxx,
follow: ["uid1","uid2","uid3",...]

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

展开
收起
落地花开啦 2016-02-27 10:13:31 7164 分享 版权
1 条回答
写回答
取消 提交回答
  • 喜欢技术,喜欢努力的人

    不要仅仅把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]}});

    2019-07-17 18:48:40
    赞同 1 展开评论