问题描述
我已经开始使用MongoDB,但我对此还很陌生.有什么方法可以对MongoDB中的文档施加约束?像指定主键还是将属性设为唯一?还是指定特定属性大于最小值?
I have started using MongoDB and I am fairly new to it.Is there any way by which I can apply constraints on documents in MongoDB?Like specifying a primary key or taking an attribute as unique?Or specifying that a particular attribute is greater than a minimum value?
推荐答案
作为无模式"数据库,您提到的某些内容必须从应用程序方面而不是数据库方面进行约束. (例如最小值")
Being a "schemaless" database, some of the things you mention must be constrained from the application side, rather than the db side. (such as "minimum value")
但是,您可以创建索引(要查询的键-请记住,查询一次只能使用一个索引,因此通常最好围绕查询设计索引,而不是仅对可能查询的每个字段进行索引反对): http://www.mongodb.org/display/DOCS/Indexes#Indexes-Basics
However, you can create indexes (keys to query on--remember that a query can only use one index at a time, so it's generally better to design your indexes around your queries, rather than just index each field you might query against):http://www.mongodb.org/display/DOCS/Indexes#Indexes-Basics
您还可以创建唯一索引,该索引将强制执行类似于唯一约束的唯一性(它确实有一些警告,例如数组字段): http://www.mongodb.org/display/DOCS/Indexes#Indexes-unique%3Atrue
And you can also create unique indexes, which will enforce uniqueness similar to a unique constraint (it does have some caveats, such as with array fields):http://www.mongodb.org/display/DOCS/Indexes#Indexes-unique%3Atrue
这篇关于如何在MongoDB中应用约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!