SchemaType.prototype.index()
Parameters
- options «Object|Boolean|String»
Returns:
- «SchemaType» this
Declares the index options for this schematype.
Example:
const s = new Schema({ name: { type: String, index: true })
const s = new Schema({ loc: { type: [Number], index: 'hashed' })
const s = new Schema({ loc: { type: [Number], index: '2d', sparse: true })
const s = new Schema({ loc: { type: [Number], index: { type: '2dsphere', sparse: true }})
const s = new Schema({ date: { type: Date, index: { unique: true, expires: '1d' }})
s.path('my.path').index(true);
s.path('my.date').index({ expires: 60 });
s.path('my.path').index({ unique: true, sparse: true });
NOTE:
Indexes are created in the background by default. If background
is set to false
, MongoDB will not execute any read/write operations you send until the index build. Specify background: false
to override Mongoose’s default.
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .