Quantcast
Channel: Mongoose, update values in array of objects - Stack Overflow
Browsing latest articles
Browse All 16 View Live

Answer by Anees Hameed for Mongoose, update values in array of objects

There is a mongoose way for doing it. const itemId = 2; const query = { item._id: itemId }; Person.findOne(query).then(doc => { item = doc.items.id(itemId ); item["name"] = "new name"; item["value"]...

View Article


Answer by Gopal Sharma for Mongoose, update values in array of objects

There is one thing to remember, when you are searching the object in array on the basis of more than one condition then use $elemMatch Person.update( { _id: 5, grades: { $elemMatch: { grade: { $lte: 90...

View Article


Answer by Chaitanya Adesara for Mongoose, update values in array of objects

update( {_id: 1, 'items.id': 2}, {'$set': {'items.$[]': update}}, {new: true}) Here is the doc about $[].

View Article

Answer by Vilintritenmert for Mongoose, update values in array of objects

In mongoose we can update, like simple array user.updateInfoByIndex(0,"test") User.methods.updateInfoByIndex = function(index, info) ={ this.arrayField[index]=info this.save() }

View Article

Answer by Howard for Mongoose, update values in array of objects

model.update({"_id": 1, "items.id": "2"}, {$set: {"items.$.name": "yourValue","items.$.value": "yourvalue"}}) Mongodb document

View Article


Answer by KARTHIKEYAN.A for Mongoose, update values in array of objects

In Mongoose, we can update array value using $set inside dot(.) notation to specific value in following way db.collection.update({"_id": args._id, "viewData._id": widgetId}, {$set:...

View Article

Answer by Shaun for Mongoose, update values in array of objects

For each document, the update operator $set can set multiple values, so rather than replacing the entire object in the items array, you can set the name and value fields of the object individually....

View Article

Answer by JohnnyHK for Mongoose, update values in array of objects

You're close; you should use dot notation in your use of the $ update operator to do that: Person.update({'items.id': 2}, {'$set': { 'items.$.name': 'updated item2', 'items.$.value': 'two updated' }},...

View Article


Mongoose, update values in array of objects

Is there a way to update values in an object? { _id: 1, name: 'John Smith', items: [{ id: 1, name: 'item 1', value: 'one' },{ id: 2, name: 'item 2', value: 'two' }] } Lets say I want to update the...

View Article


Answer by Pedro JR for Mongoose, update values in array of objects

Having tried other solutions which worked fine, but the pitfall of their answers is that only fields already existing would update adding upsert to it would do nothing, so I came up with this....

View Article

Answer by Jakub A Suplicki for Mongoose, update values in array of objects

Below is an example of how to update the value in the array of objects more dynamically.Person.findOneAndUpdate({_id: id}, { "$set": {[`items.$[outer].${propertyName}`]: value} },{ "arrayFilters": [{...

View Article

Answer by Taban Cosmos for Mongoose, update values in array of objects

I had similar issues. Here is the cleanest way to do it. const personQuery = { _id: 1 } const itemID = 2; Person.findOne(personQuery).then(item => { const audioIndex = item.items.map(item =>...

View Article

Answer by samehanwar for Mongoose, update values in array of objects

cleaner solution using findOneAndUpdate await Person.findOneAndUpdate( { _id: id, 'items.id': 2 }, { $set: {'items.$.name': 'updated item2', 'items.$.value': 'two updated', } }, );

View Article


Answer by heavy-matill for Mongoose, update values in array of objects

I needed to update an array element with dynamic key-value pairs.By mapping the update object to new keys containing the $ update operator, I am no longer bound to know the updated keys of the array...

View Article

Answer by Cuado for Mongoose, update values in array of objects

Found this solution using dot-object and it helped me.import dot from "dot-object";const user = await User.findByIdAndUpdate(id, { ...dot.dot(req.body) });

View Article


Answer by bho0t for Mongoose, update values in array of objects

person.updateMany( { "items.id": id }, { $set: {"items.$.name": name,"items.$.value": value, }, }, { new: true }) .then((update) => {res.send(update)}). catch((err)=>{res.send(err)})

View Article
Browsing latest articles
Browse All 16 View Live


Latest Images