Mongo(ose)是否仍然支持字段级唯一性约束? [英] Are field level uniqueness constraints still supported in Mongo(ose)?

查看:650
本文介绍了Mongo(ose)是否仍然支持字段级唯一性约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在早期版本中,mongoose支持 field 级唯一性约束,如下所示(见于2.7.x 此处):

It looks like mongoose supported field level uniqueness constraints in earlier versions as follows (seen in 2.7.x here):

var SomeSchema = new Schema ({
  field: {index: {unique: true}} // field level
})

我在4.4.x文档中找不到任何引用。现在是在架构级别执行此操作的首选方法,如此(见于4.4.x 这里):

I can't find any reference to it in the 4.4.x docs. Is the preferred way to do this on the schema level now, like so (seen in 4.4.x here):

SomeSchema.index({field: 1}, {unique: true}) // schema level


推荐答案

是的,唯一索引是仍然支持Mongoose;请参阅此处此处

Yes, unique indexes are still supported in Mongoose; see here and here in the current docs.

来自链接文档的示例:

var s = new Schema({ date: { type: Date, index: { unique: true, expires: '1d' }});
var s = new Schema({ name: { type: String, unique: true }});

Schema.path('my.path').index({ unique: true, sparse: true });
Schema.path('name').index({ unique: true });

这篇关于Mongo(ose)是否仍然支持字段级唯一性约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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