Skip to content Skip to sidebar Skip to footer

Check If Document Already Exists And If Yes Then Update, Otherwise Create New - Mongoose

I have app where I am trying to check whether a document with the users ID already exists, if yes then update the existing one with the data sent in the request if it does not exis

Solution 1:

Like Alexander Mac says in the comments, you are overriding the array yourself. If the document exists, try:

if (bio) {
    console.log("Bio document for user already exists!");
    bio.userBios.push({
        background: req.body.userBios.background,
        experience: req.body.userBios.experience,
        skills: req.body.userBios.skills,
        bioForSector: req.body.userBios.bioForSector
    });
    bio.save(function(err) {
        if (err)
            throw err
        return (null, bio)
    });
}

This way you really push it in an existing array instead of creating a new one.

Post a Comment for "Check If Document Already Exists And If Yes Then Update, Otherwise Create New - Mongoose"