我正在尝试使用Mongoose为每个用户强制执行一个唯一的属性(电子邮件)。有人可以解释MongooseJS中以下两行之间的区别(它们是否都是有效的语句)?
var userSchema = new Schema({ email: { type: String, index: { unique: true }}});
var userSchema = new Schema({ email: { type: String, index: true, unique: true }});
谢谢!
答案 0 :(得分:0)
在电子邮件字段中使用这样的独特
var mongoose = require('mongoose');
Schema = mongoose.Schema;
var UserSchema = new mongoose.Schema({
name: {type: String, required:true},
mobileNumber: {type: String, required: true},
email: {type: String, required: true, unique: true}
});
var User = mongoose.model('User', UserSchema);
module.exports = User;