query - mongodb如何进行子查询
怪我咯
怪我咯 2017-04-21 11:16:39
0
1
785

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

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

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

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

走同样的路,发现不同的人生

reply all(1)
黄舟

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]}});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template