简体   繁体   中英

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?

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 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM