createIndex()

    db.collectionspace.collection.createIndex(<name>,<indexDef>,[options])

    参数描述

    属性名参数类型描述默认值
    UniqueBoolean索引是否唯一。false
    EnforcedBoolean索引是否强制唯一。false
    NotNullBoolean索引的任意一个字段是否允许为 null 或者不存在。false
    SortBufferSizeint创建索引时使用的排序缓存的大小。64MB

    返回值

    错误码

    示例

    • 集合 employee 创建唯一索引,并且索引字段不允许为 null 或者不存在 。

      1. >
      2. > // b 字段为 null,插入索引时报错
      3. Any field of index key should exist and cannot be null
      4. Takes 0.002531s.
      5. >
      6. > db.sample.employee.insert( { a: 1 } )
      7. sdb.js:625 uncaught exception: -339