Arrays
Say, you got a JSON like this, and you want to add to the albums array
{
"username": "Kanye West",
"type": "Human",
"profession": "Artist",
"albums": [],
"_id": "154f6259-4d8b-4a60-888c-dade18524003"
}
You would do something like this
db.find({username: "Kanye West"})
.then((data) => {
data.result[0].albums.push("The College Dropout")
data.result[0].albums.push("Late Registration")
data.result[0].albums.push("Graduation")
data.result[0].albums.push("The Blueprint")
data.updateOne(0).then((data) => {
console.log(data)
})
})
Problem solved!
This is where having a Data class really helps, you can interact and update the data with the methods you already know.
Wait, what? I just remembered, "The Blueprint" isn't a Kanye West problem, I think I can fix this..
db.find({username: "Kanye West"})
.then((data) => {
data.result[0].albums.pop()
data.updateOne(0).then((data) => console.log(data))
})
Okay, fixed!