Currently, I have a basic collection stored in MongoDB using Mongoose.
My users model consists of a single field of type object, and I am looking to dynamically modify this object. However, despite my attempts using findByIdAndUpdate()
, findById
, findOne()
, and findOneAndUpdate()
, the code does not seem to be working as expected.
const UsersSchema = mongoose.Schema({
likes: {}
},
{ collection: 'users' });
const Users = mongoose.model('Users', UsersSchema);
const id ="5b4c540f14f353a4b9875af4";
const themes = ['foo', 'bar'];
Users.findById(id, (err, res) => {
themes.map(item => {
if (res.likes[item]) {
res.likes[item] = res.likes[item] + 1;
} else {
res.likes[item] = 1;
}
});
res.save();
});