Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose upsert duplicate key error

I'm trying do an upsert using Mongoose, but I'm getting a duplicate key error for the very key that should trigger the upsert.

Schema:

"resource": {type: Schema.ObjectId, ref: "Resource"},                       
"skill": {type: Schema.ObjectId, ref: "Skill"},                             
"level": {type: Number, min: 1, max: 5}

.index({skill: 1, resource: 1}, {unique: true});

Then I make the call:

    //self is a Resource instance
    ResourceSkillLevel.update({                                           
        resource: self._id,
        skill: skill._id,
        level: level
    }, {$set: {level: level}}, {upsert: true}, cb);

If the (resource, skill) does not exist, this call works just fine and properly creates the ResourceSkillLevel entry. However, when I call it again I get duplicate key error index. The listed duplicate key is the tuple resource/skill key. Why isn't it upserting when it finds the duplicate?

like image 227
Explosion Pills Avatar asked Mar 22 '23 21:03

Explosion Pills


1 Answers

You're including level in the update query criteria parameter, so if that doesn't also match the existing record it will try and create a new doc which will fail the unique index which only spans skill and resource.

Try changing your update to this:

ResourceSkillLevel.update({                                           
    resource: self._id,
    skill: skill._id
}, {$set: {level: level}}, {upsert: true}, cb);
like image 83
JohnnyHK Avatar answered Apr 02 '23 13:04

JohnnyHK