Mongoose duplicates with the schema key unique

后端 未结 1 1402
情话喂你
情话喂你 2021-01-31 16:28

I want to make the key project unique across that collection but i cant getting this working, i found similar problem here.

task.js

function make(Schema,         


        
相关标签:
1条回答
  • 2021-01-31 17:21

    The Schema object you're passing may not work correctly because you are nesting 'unique' attribute into 'index' attribute, try something like this (it works as intended) :

    User = mongoose.model('User', new Schema({
        firstName:  {
            type:String,
            required: true,
        },
        lastName: {
            type:String,
            required: true,
        },
        email: {
            type:String,
            required: true,
            unique: true
        },
        address: String,
        phone: {
            type:String,
            required: true,
        },
        password:  {
            type:String,
            required: true,
            set: Data.prototype.saltySha1 // some function called before saving the data
        },
        role: String
    },{strict: true}));
    

    Or more specifically for your example :

    var Tasks = new Schema({
        project: { 
            type: String, 
            unique: true,
            index: true
        },
        description: String
    });
    

    Note : I don't know what you're trying to do with the "dropDups" parameter, it doesn't seems to be in the mongoose documentation.

    0 讨论(0)
提交回复
热议问题