设置对mongo db收集的限制 [英] Set limits on mongo db collection

查看:105
本文介绍了设置对mongo db收集的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想在mondo db中设置两个限制:

So, there are two limits I want to set in mondo db:

1.只允许将一个文档插入数据库,不能再插入.该文档一旦添加便无法删除,但可以修改.另外,不能将其他文档添加到该集合中.

在此架构下仅允许一个值.

Only one value will be allowed under this schema.

{ "_id" : ObjectId("5800"), "seconds" : "120", "__v" : 0 }

不允许再添加新的秒数,并且只能修改以上文档.

No more new seconds will be allowed to be added in, and only the above document can be modified.

我尝试过:

var numbersSchema = new mongoose.Schema({
  seconds: { type: Number, min: 60 }
},{ capped : true, size:4000,  max : 1 })

但是我仍然可以添加多个文档:

However I can still add multiple documents:

{ "_id" : ObjectId("5800c7f53c609009dc5800f4"), "seconds" : 390, "__v" : 0 }
{ "_id" : ObjectId("5800c81b3c609009dc5800f5"), "seconds" : 590, "__v" : 0 }

2.在模式输入字段上设置最小值.

var numbersSchema = new mongoose.Schema({
  seconds: Number
})

因此,在这种情况下,秒"必须至少为60秒作为最小值.我认为这需要在架构中进行更改,但我不知道如何做.是否可以在架构中添加> 59",还是mongo已经有一个选项?

So, in this case 'seconds' must be at least 60 seconds as the minimum value. I assume this needs to be changed in the schema, but I do not know how. Is it possible to add '>59' into the schema, or is there an option with mongo already?

任何提示将不胜感激!

Any tips would be appreciated!

推荐答案

使用

This can be easily achieved using Capped Collections in mongodb. you can set a limit on number of documents, so in your case you can set maximum number of documents to 1 as below:

db.createCollection("numbers", { capped : true, size:4000,  max : 1 } )

如果您想将集合设置为猫鼬模式的上限,则可以使用capped选项,如下所示:

Also if you want to set your collection as capped from mongoose schema, you can do it using capped option as follows:

var numbersSchema = new mongoose.Schema({
  seconds: Number
},{ capped : true, size:4000,  max : 1 })

此收藏集将始终仅包含一个可以更新的文档.请注意,一旦将文档插入上限集合中,则不能删除 .因此,这也解决了您的删除问题.


另外,您需要指定的size选项可以是任何正整数值,该值将在内部由mongo转换为最小值4096 bytes或最接近的256倍.

This collection will always hold only one document which can be updated. Note that the documents once inserted in capped collections can not be deleted. So this solves your deletion problem too.


Also the size option that you need to specify, can be any positive integer value, which will be converted internally by mongo to a minumum of 4096 bytes, or closest multiple of 256.

重要提示:

始终必须使用size参数,即使您指定最大数量 文件.如果集合,MongoDB将删除较旧的文档 在达到最大文档数之前达到最大大小限制 计数.

The size argument is always required, even when you specify max number of documents. MongoDB will remove older documents if a collection reaches the maximum size limit before it reaches the maximum document count.

来源: MONGODB文档


另外,如果要将最小值或最大值设置为秒,则可以始终使用mongoose min-max 验证.因此,以下架构应该适合您.

Source: MONGODB docs


Also, if you want to set the min or max value to the seconds, you can always use mongoose min-max validations. So the following schema should work for you.

var numbersSchema = new mongoose.Schema({
  seconds: { type: Number, min: 60 }
},{ capped : true, size:4000,  max : 1 })

这篇关于设置对mongo db收集的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆