简体   繁体   English

如何在mongodb中构建此数据库?

[英]How to construct this database in mongodb?

say i have two collections in mongodb,one for users which contains users basic info,and one for apps which contains applications.now if users are allowed to add apps,and the next time when they login, the web should get the user added app for them.how should i construct this kind of database in mongodb. 说我在mongodb中有两个集合,一个集合用于包含用户基本信息的用户,另一个集合用于包含应用程序的应用程序。现在,如果允许用户添加应用程序,并且下次他们登录时,网络应该让用户添加应用程序为他们。我应该如何在mongodb中构建这种数据库。

users:{_id:ObjectId(),username:'username',password:'password'}
apps:{_id:ObjectId(),appname:'',developer:,description:};

and how should i get the user added app for them??? 以及如何为用户添加用户应用??? should i add something like addedAppId like: 我是否应该添加诸如addedAppId之类的内容:

users:{_id:ObjectId(),username:'username',password:'password',addedApppId:[]}

to indicate which app they have added,and then get the apps using addedAppId ??? 指示他们已经添加了哪个应用,然后使用addedAppId来获取应用?

Yep, there's nothing wrong with the users collection keeping track of which apps a user has added like you've indicated. 是的, users集合没有什么不妥, users跟踪users按照您的指示添加了哪些应用。

This is known as linking in MongoDB. 这在MongoDB中称为linking In a relational system you'd probably create a separate table, added_apps , which had a user ID, app ID and any other relevant information. 在关系系统中,您可能会创建一个单独的表added_apps ,该表具有用户ID,应用程序ID和任何其他相关信息。 But since you can't join, keeping this information in the users collection is entirely appropriate. 但是由于您无法加入,因此将这些信息保留在users集合中是完全适当的。

From the docs : 文档

A key question when designing a MongoDB schema is when to embed and when to link. 设计MongoDB模式时的关键问题是何时嵌入和何时链接。 Embedding is the nesting of objects and arrays inside a BSON document. 嵌入是BSON文档中对象和数组的嵌套。 Links are references between documents. 链接是文档之间的引用。

There are no joins in MongoDB – distributed joins would be difficult on a 1,000 server cluster. MongoDB中没有联接-在1,000个服务器群集上很难进行分布式联接。 Embedding is a bit like "prejoined" data. 嵌入有点像“预连接”数据。 Operations within a document are easy for the server to handle; 文档中的操作很容易由服务器处理。 these operations can be fairly rich. 这些操作可以相当丰富。 Links in contrast must be processed client-side by the application; 相反,链接必须由应用程序在客户端进行处理; the application does this by issuing a follow-up query. 应用程序通过发出后续查询来做到这一点。

(this is the extra bit you'd need to do, fetch app information from the user's stored AppId .) (这是您需要做的额外工作,从用户存储的AppId获取应用程序信息。)

Generally, for "contains" relationships between entities, embedding should be be chosen. 通常,对于实体之间的“包含”关系,应选择嵌入。 Use linking when not using linking would result in duplication of data. 不使用链接时使用链接将导致数据重复。

... ...

Many to many relationships are generally done by linking. 多对多关系通常是通过链接来完成的。

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

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