Willem van der Veen
Willem van der Veen

Reputation: 36620

MongoDB updating an object

Currently working on a NodeJS backend with mongoDB. I'm trying to update an Object in mongoDB using NodeJS driver:

 "mongodb": "^3.0.2",

I am using the findOneAndUpdate query and have tried the following syntax:

First syntax:

updatedPlayerData = await db.db(MDBC.db).collection(MDBC.pC).findOneAndUpdate({
    'username': req.body.username
}, {
        $set: {
            [profession.city]: '',
            [profession.organisation]: '',
            [profession.profession]: ''
        }
    }, { returnOriginal: false });

Second syntax:

updatedPlayerData = await db.db(MDBC.db).collection(MDBC.pC).findOneAndUpdate({
    'username': req.body.username
}, {
        $set: { 
            profession: {
                city: '',
                organisation: '',
                profession: ''
            }
        }
    }, { returnOriginal: false });

Also have tried a bunch of other stuff. Can't seem to update the object properly. How can I properly update an object?

Upvotes: 0

Views: 65

Answers (1)

Parth Mahajan
Parth Mahajan

Reputation: 136

You can try this :

db.db(MDBC.db).collection(MDBC.pC).findOneAndUpdate({
    'username': req.body.username
}, {
        $set: {
            'profession.city': '',
            'profession.organisation': '',
            'profession.profession': ''
        }
    }, { returnOriginal: false });

Upvotes: 2

Related Questions