createIndex()
db.collectionspace.collection.createIndex(<name>,<indexDef>,[options])
参数描述
属性名 | 参数类型 | 描述 | 默认值 |
---|---|---|---|
Unique | Boolean | 索引是否唯一。 | false |
Enforced | Boolean | 索引是否强制唯一。 | false |
NotNull | Boolean | 索引的任意一个字段是否允许为 null 或者不存在。 | false |
SortBufferSize | int | 创建索引时使用的排序缓存的大小。 | 64MB |
返回值
示例
集合 employee 创建唯一索引,并且索引字段不允许为 null 或者不存在 。
>
> // b 字段为 null,插入索引时报错
Any field of index key should exist and cannot be null
Takes 0.002531s.
>
> db.sample.employee.insert( { a: 1 } )
sdb.js:625 uncaught exception: -339