簡體   English   中英

在參考貓鼬中創建唯一索引

[英]Create Unique Index in ref Mongoose

我有這些架構:

var userSchema = new mongoose.Schema({
    username: {type: String,unique: true}
});

mongoose.model( 'User', userSchema );


var fooSchema = new mongoose.Schema({
    title : {type: String,trim: true},
    owner : {type: mongoose.Schema.Types.ObjectId, ref: 'User'}
});

fooSchema.index({ title: 1, owner: 1 }, { unique: true }); //does not work
mongoose.model( 'Foo', fooSchema);

我要使用驗證,那么每個foo都可以具有唯一用戶的唯一標題

我嘗試過

fooSchema.index({ title: 1, owner: 1 }, { unique: true });

但是如果另一個用戶創建具有相同標題的新foo,我也會出錯

更新:我添加了unique: trueusername ,但我有同樣的問題

對於這個休眠線程的新觀眾:

mySchema.index({field1: 1, field2: 1}, {unique: true});

看到這個線程:

在Mongoose / MongoDB中創建多字段索引

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM