Mangusta, aktualizacja wartości w tablicy obiektów

Czy istnieje sposób na aktualizację wartości w obiekcie?

{
  _id: 1,
  name: 'John Smith',
  items: [{
     id: 1,
     name: 'item 1',
     value: 'one'
  },{
     id: 2,
     name: 'item 2',
     value: 'two'
  }]
}

Powiedzmy, że chcę zaktualizować nazwę i wartości pozycji dla pozycji, gdzie id = 2;

Wypróbowałem następujące w / Mangusta:

var update = {name: 'updated item2', value: 'two updated'};
Person.update({'items.id': 2}, {'$set':  {'items.$': update}}, function(err) { ...

Problem z tym podejściem polega na tym, że aktualizuje / ustawia cały obiekt, dlatego w tym przypadku tracę pole id.

Czy jest lepszy sposób w mongoose, aby ustawić pewne wartości w tablicy, ale pozostawić inne wartości w spokoju?

Zapytałem również o Osoba:

Person.find({...}, function(err, person) {
  person.items ..... // I might be able to search through all the items here and find item with id 2 then update the values I want and call person.save().
});
Author: aaaidan, 2013-03-29

6 answers

Jesteś blisko; powinieneś użyć notacji kropkowej w swoim $set, aby to zrobić:

Person.update({'items.id': 2}, {'$set': {
    'items.$.name': 'updated item2',
    'items.$.value': 'two updated'
}}, function(err) { ...
 88
Author: JohnnyHK,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-28 20:54:08
model.update({"_id": 1, "items.id": "2"}, 
{$set: {"items.$.name": "yourValue","items.$.value": "yourvalue"}})

Dokument Mongodb

 6
Author: Howard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-14 03:56:47

Dla każdego dokumentu operator aktualizacji $set Może ustawić wiele wartości , więc zamiast zastępować cały obiekt w tablicy items, można ustawić pola name i value obiektu indywidualnie.

{'$set':  {'items.$.name': update.name , 'items.$.value': update.value}}
 4
Author: Shaun,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-28 20:55:29

W mongoose możemy zaktualizować, jak prosta tablica

user.updateInfoByIndex(0,"test")

User.methods.updateInfoByIndex = function(index, info) ={
    this.arrayField[index]=info
    this.save()
}
 0
Author: Vilintritenmert,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-08 13:38:32

W Mongoose możemy zaktualizować wartość tablicy używając {[1] } wewnątrz notacji dot (.) do określonej wartości w następujący sposób

db.collection.update({"_id": args._id, "viewData._id": widgetId}, {$set: {"viewData.$.widgetData": widgetDoc.widgetData}})
 0
Author: KARTHIKEYAN.A,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-02 21:57:35
update(
    {_id: 1, 'items.id': 2},
    {'$set': {'items.$[]': update}},
    {new: true})

Oto doc o $[].

 0
Author: Chaitanya Adesara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-13 13:53:11