1

In nedb, I have an array field in a document. How will I update an array element at any index?

For example,

{  
  fruits:['mango','apple','banana']
}

I would like to modify the 2nd element and make the array as ['mango','pear','banana'].

How to do it using db.update?

Sujeet Sinha
  • 2,417
  • 2
  • 18
  • 27

2 Answers2

1

You can do it like this:

db.update({_id:idToUpdate}, {
    $set:{'fruits[1]':'pear'}
}, {}, callback);
Rudi
  • 2,987
  • 1
  • 12
  • 18
1

You can use dot notation with index:

db.update({_id: id, {$set: {'fruits.1': 'pear'}}, {}, callback)

One thing though, you have to be sure that you using the right index.