简体   繁体   English

MongoDB收集架构

[英]MongoDB collection schema

I'm a 1 day beginner in MongoDB and I try to not think the classic relational way, which seems pretty harder in the implementation than what I thought. 我是MongoDB的1天初学者,我尝试不考虑经典的relational方式,这在实现上似乎比我想象的要难得多。

what I want to achieve is a simple database containing 2 collections : User - Interest 我要实现的是一个包含2个集合的简单数据库: 用户 - 兴趣

a user might have many interests. 用户可能有很多兴趣。

this is what I have now : 这就是我现在所拥有的:

User : 用户名:

{
    _id: 'user1',
    language: 'EN', 
    firstName: 'Mehdi',
    interests: [
        interest1, 
        interest2, 
        interest3
    ]
}

Interest : 兴趣:

{
    _id: 'interest1',
    name: 'Mobile'
}

So I'm a little bit confused here, 所以我有点困惑

should I store first store Interest documents in the Interest Collection then retrieve them as Objects and store them as Binary Objects in User documents ? 我应该先将兴趣文档存储在兴趣集合中,然后将其检索为对象,然后将其存储为用户文档中的Binary Objects吗? so I will have something like : Create Interest Documents --> Retrieve them --> create Users document and Store the interests on it ? 所以我会有类似的东西: 创建兴趣文档 -> 检索它们 -> 创建用户文档并在其中存储兴趣

Or should I simply nest Interest documents in the User document without create them before. 还是应该只将兴趣文档嵌套在用户文档中,而无需之前创建它们。 so I will have something like : Create User Documents and create/nest Interests document on it ? 所以我会有类似的内容: 创建用户文档并在其上创建/嵌套兴趣文档

Thank you. 谢谢。

I dont know your exact requirements, but I would probably use just the "users" collection, and embed interests the way you have. 我不知道您的确切要求,但我可能只会使用“用户”集合,并以您的方式嵌入兴趣。

If you want a list of all the different interests, you can try: 如果要列出所有不同兴趣的列表,可以尝试:

db.users.distinct("interests");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM