search

Home  >  Q&A  >  body text

query - mongodb如何进行子查询

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

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

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

<uid1, name1, description1>
<uid2, name2, description2>
...
怪我咯怪我咯2767 days ago756

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-21 11:18:39

    Don’t just think of mongodb as a schemeless SQL database. Mongodb does not have the concepts of subqueries and cross-table queries.

    According to your description, if you want to get the detailed information list of everyone followed by a user, one way is to save all the information of these users in User:

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

    Or you can use secondary query and check again in the code

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

    reply
    0
  • Cancelreply