简体   繁体   English

在mongodb中的数据库中使用不同的集合是否好

[英]is it good to use different collections in a database in mongodb

I am going to do a project using nodejs and mongodb. 我将使用nodejs和mongodb做一个项目。 We are designing the schema of database, we are not sure that whether we need to use different collections or same collection to store the data. 我们正在设计数据库的架构,我们不确定是否需要使用不同的集合或相同的集合来存储数据。 Because each has its own pros and cons. 因为每个人都有自己的优点和缺点。 If we use single collection, whenever the database is invoked, total collection will be loaded into memory which reduces the RAM capacity.If we use different collections then to retrieve data we need to write different queries. 如果我们使用单个集合,那么每当调用数据库时,总集合将被加载到内存中,这会减少RAM容量。如果我们使用不同的集合,则要检索数据,我们需要编写不同的查询。 By using one collection retrieving will be easy and by using different collections application will become faster. 通过使用一个集合,检索将很容易,而通过使用不同的集合,应用程序将变得更快。 We are confused whether to use single collection or multiple collections. 我们对于使用单个集合还是多个集合感到困惑。 Please Guide me which one is better. 请指导我哪个更好。

Usually you use different collections for different things. 通常,您将不同的集合用于不同的事物。 For example when you have users and articles in the systems, you usually create a "users" collection for users and "articles" collection for articles. 例如,当您在系统中有用户和文章时,通常为用户创建“用户”集合,为文章创建“文章”集合。 You could create one collection called "objects" or something like that and put everything there but it would mean you would have to add some type fields and use it for searches and storage of data. 您可以创建一个称为“对象”或类似名称的集合,然后将所有内容放置在其中,但这意味着您必须添加一些类型字段并将其用于搜索和数据存储。 You can use a single collection in the database but it would make the usage more complicated. 您可以在数据库中使用单个集合,但这会使使用更加复杂。 Of course it would let you to load the entire collection at once but whether or not it is relevant for the performance of your application, that is something that would have to be profiled and tested to give your the performance impact for your particular use case. 当然,它可以让您立即加载整个集合,但是无论它与应用程序的性能是否相关,都必须对其进行概要分析和测试,以对特定用例产生性能影响。

Usually, developers create the different collection for different things. 通常,开发人员为不同的事物创建不同的集合。 Like for post management, people create 'post' collection and save the posts in post collection and same for users and all. 就像帖子管理一样,人们创建“帖子”集合并将帖子保存在帖子集合中,并且对用户和所有人都是相同的。

Using different collection for different purpose is a good pratices. 将不同的集合用于不同的目的是一个很好的习惯。

MongoDB is great at scaling horizontally. MongoDB擅长水平扩展。 It can shard a collection across a dynamic cluster to produce a fast, querable collection of your data. 它可以在动态集群上分片集合,以生成快速,可查询的数据集合。

So having a smaller collection size is not really a pro and I am not sure where this theory comes that it is, it isn't in SQL and it isn't in MongoDB. 因此,具有较小的集合大小并不是真正的专家,而且我不确定该理论从何而来,它不在SQL中,也不在MongoDB中。 The performance of sharding, if done well, should be relative to the performance of querying a single small collection of data (with a small overhead). 分片的性能(如果处理得当的话)应该与查询单个小的数据集合(开销很小)的性能有关。 If it isn't then you have setup your sharding wrong. 如果不是,则说明分片设置错误。

MongoDB is not great at scaling vertically, as @Sushant quoted, the ns size of MongoDB would be a serious limitation here. MongoDB不能很好地垂直扩展,正如@Sushant所引用的那样,MongoDB的ns大小将是一个严重的限制。 One thing that quote does not mention is that index size and count also effect the ns size hence why it describes that: 引用中没有提到的一件事是索引大小和计数也会影响ns大小,因此为什么要这样描述:

By default MongoDB has a limit of approximately 24,000 namespaces per database. 默认情况下,每个数据库的MongoDB限制为大约24,000个名称空间。 Each namespace is 628 bytes, the .ns file is 16MB by default. 每个命名空间为628个字节,.ns文件默认为16MB。

Each collection counts as a namespace, as does each index. 每个集合以及每个索引都计为一个名称空间。 Thus if every collection had one index, we can create up to 12,000 collections. 因此,如果每个集合都有一个索引,那么我们最多可以创建12,000个集合。 The --nssize parameter allows you to increase this limit (see below). 使用--nssize参数可以增加此限制(请参见下文)。

Be aware that there is a certain minimum overhead per collection -- a few KB. 请注意,每个集合有一定的最低开销-几个KB。 Further, any index will require at least 8KB of data space as the b-tree page size is 8KB. 此外,任何索引都将至少需要8KB的数据空间,因为b树页面大小为8KB。 Certain operations can get slow if there are a lot of collections and the meta data gets paged out. 如果有很多集合,并且分页出元数据,则某些操作可能会变慢。

So you won't be able to gracefully handle it if your users exceed the namespace limit. 因此,如果用户超出名称空间限制,您将无法正常处理它。 Also it won't be high on performance with the growth of your userbase. 而且,随着用户群的增长,它的性能也不会很高。

UPDATE UPDATE

For Mongodb 3.0 or above using WiredTiger storage engine, it will no longer be the limit. 对于使用WiredTiger存储引擎的Mongodb 3.0或更高版本,它将不再是限制。

Yes personally I think having multiple collections in a DB keeps it nice and clean. 是的,我个人认为数据库中包含多个集合可以使其保持整洁。 The only thing I would worry about is the size of the collections. 我唯一担心的是集合的大小。 Collections are used by a lot of developers to cut up their db into, for example, posts, comments, users. 许多开发人员都使用集合来将他们的数据库划分为例如帖子,评论,用户。

Sorry about my grammar and lack of explanation I'm on my phone 很抱歉我的语法和缺乏解释,我在手机上

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

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