M
M
Michael2017-01-16 22:24:59
MongoDB
Michael, 2017-01-16 22:24:59

How to properly update data in MongoDB?

Hello. The data is stored in this way

{
 id: //,
 list: ['one', 'two', 'three']
}

I get an array ['four', 'five']. Is it possible somehow to update the data with one command so that the list field contains [one', 'two', 'three', 'four', 'five']?
So far, only such a solution comes to mind: read the data, add two arrays, overwrite. Working with MongoDB via node.js (native driver)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yaroslav, 2017-01-17
@mak_ufo

$push just adds the data to the array
$addToSet adds with the replacement
tag = ['four', 'five']
db.collection.update({searchtags: "anything"}, {$push: {tag: ['one', ' two', 'three', 'four']}})
tag = ['one', 'two', 'three', 'four', 'four', 'five']
db.collection.update({searchtags: "anything"}, {$addToSet: {tag: ['one', 'two', 'three', 'four']}})
tag = ['one', 'two', 'three', 'four' , 'five']
Through a dot, you can push directly into the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question