From the course: Learning MongoDB

Unlock the full course today

Join today to access over 23,400 courses taught by industry experts.

Updating arrays

Updating arrays

- [Instructor] Working with fields that contain array data means we will need to use some different operators, push and pull. To add an item to an array, we can use push, which will push an item to the end of the array. So, let's start off again with our Tacos recipe, and look again at that likes array. So there's two items in it, two user IDs. Let's go ahead and push another user ID into that array. So we can do db.examples, updateOne, tell it we want to update where the title is Tacos, and as the second parameter, we're going to say we want to push something into the likes array. And in this case, let's add ID 60, and then we'll close that out. You'll see it matched one document, modified one document. If we run our find again, you'll see there is now a 60 added in our likes array. If we wanted to remove that, we can simply use the pull operator. So instead of push here, we can just change that to pull. It will,…

Contents