Updating Properties
Say, you got a JSON like this, and you want to change the type of them
{
"username": "Vito Corleone",
"type": "Human",
"_id": "5fc4e2e1-b84a-4934-87d0-f8ca595e63f2"
},
{
"username": "Tom Hagen",
"type": "Human",
"profession": "Lawyer",
"_id": "d0db9648-215e-44a6-a974-669a02337338"
},
{
"username": "Micheal Corleone",
"type": "Human",
"_id": "577c1352-7e8d-4716-89e0-7b1e28e8c3ec"
},
{
"username": "Kay",
"type": "Human",
"_id": "8ae63a7d-7665-4d90-8391-cd91ef3d8702"
}
You can do it like this
db.find({type: "Human"}).then((d) => {
d.result.forEach((e) => e.type = "Fictional Characters")
d.updateAll().then((d) => console.log(d))
})
Properties updated!
Adding Properties
That's really easy too.
You can do it like this
db.find({type: "Human"}).then((d) => {
d.result.forEach((e) => e.legs = 2)
d.updateAll().then((d) => console.log(d))
})
This will update all the type: "Humans"
objects with a property of legs and its value will be 2. Handy, right?
Deleting Properties
Want to delete properties? Set them to undefined
!
db.find({ username: "Kanye West" })
.then((data) => {
data.result[0].type = undefined
data.updateAll()
})
"There you go! Now you know everything" Someone once said to me. But of course I didn't knew everything. but yeah uh lets move on...