this is my mongodb object
{
"_id" : ObjectId("4faaba123412d654fe83hg876"),
"user_id" : 123456,
"posters" : [ 123456,1111,456789,3333]
}
i want to add an item to posters array, or remove one how could i do it?
its nested updating.
i saw some question around stackoverflow, but i didnt found any answer how to remove object from the array, lets say 3333 there.
so the result will be:
{
"_id" : ObjectId("4faaba123412d654fe83hg876"),
"user_id" : 123456,
"posters" : [ 123456,1111,456789]
}
Use$pull .
db.collection.update(
{ posters: "3333" },
{ $pull: { posters: "3333" } },
{ multi: true }
)
Use $push to add something into an array:
db.collection.update(
{ $push:
{
"posters" : "0000"
}
}
)
Use $pull to remove something from an array:
db.collection.update(
{ $pull:
{
"posters" : "3333"
}
}, True
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.