Started by Shaleena AgarwalSep 7, 2021

Open
How to update MongoDB with unknown object

0 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWES SHARE

I'm using the following Mongoose model:

const userSchema = new mongoose.Schema({
userID: { type: String, require: true, unique: true },
username: { type: String },
serverID: { type: String, require: true },
roles: [String],
gameProfile: {
accountName: { type: String, default: '?' },
accountLevel: { type: Number, min: 1, max: 65, default: 1 },
accountClass: { type: String, default: '?' },
favoriteWeapon: { type: String, default: '?' },
},
}, { timestamps: true });

const User = mongoose.model('User', userSchema);

And I'm trying to create a "gameProfile" edit command, so the user can update their accountName, accountClass, etc. Currently, when the user runs my edit command, I receive an object of the data they want to edit (all in one shot) that may look something like: { accountName: 'bob', accountLevel: 55 } or { accountName: 'billy', favoriteWeapon: 'axe', accountLevel: 100, accountClass: 'TANK'} or any permutation of those fields in the gameProfile object.

So what I need to happen is basically merge that object into the existing User document, in one update. Without updating the values that weren't edited.

What I naively tried was this:

// const data = { accountLevel: 50, accountClass: 'DPS', favoriteWeapon: 'sword' }

UserModel.updateOne({ userID: interaction.user.id }, { data }, function(err, res) {
if (err) {
console.error(err);
} else {
console.log('RESULT: ', res);
}
});

Which doesn't error, but also doesn't update the database. If anyone can help me out it'd be much appreciated. I'm just learning MongoDB/NoSQL, thank you

0 Replies

You must be Logged in to reply
Techiio

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

facebooklinkdeintwitter

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Copyright techiio.com @2020 Kolkata, India
made with by Abhishek & Priyanka Jalan
Copyright techiio.com @2020
made with by Abhishek & Priyanka Jalan