af_159623
af_159623

Reputation: 327

UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection

I'm trying to update an object inside an array on a document

    "state": [],
    "users": [{
        "ready": false,
        "_id": {
            "$oid": "5fb810c63af8b34180912014"
        },
        "user": {
            "$oid": "5f81eb91d537dc3baf443a84"
        },
        "calification": 0
    }, {
        "ready": false,
        "_id": {
            "$oid": "5fb810ca3af8b34180912015"
        },
        "user": {
            "$oid": "5fa6f98f15e96c1125b905a9"
        },
        "calification": 0
    }],
    "test": {
        "$oid": "5f986af2baa88b2d30760961"
    },
    "__v": 1
}

and I'm using this function

updateUserState = async function(game_id,username){
    var user_id = '5fb7fae0c28f1d33a99b877e';
    await Game.findOneAndUpdate({_id: game_id, users: {$elemMatch: {user: user_id}}},
        {$set: { ready: true}}, 
        {'new': true, 'safe': true, 'upsert': true});
}

but I get this error

(node:16768) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: myproject.games index: _id_ dup key: { _id: ObjectId('5fb810c63af8b34180912013') }

I don't know why is telling that the key is duplicate

Upvotes: 0

Views: 99

Answers (1)

Rıdvan Can
Rıdvan Can

Reputation: 89

{upsert: true, new: true, runValidators: true}

You can try as above

Upvotes: 0

Related Questions