Create a gist now

Instantly share code, notes, and snippets.

mongodb push / pushall / pop
var document = { name: "jakub", surname: "oboza", games: []};
db.people.save(document);
var person = db.people.findOne({name: "jakub"});
person
{
"_id" : ObjectId("4f65da9d8199c991088c9ca4"),
"name" : "jakub",
"surname" : "oboza",
"games" : [ ]
}
db.people.update({_id: person._id}, { $pushAll: {games: ["Skyrim", "Fallout New Vegas"]} });
var person = db.people.findOne({name: "jakub"});
person
{
"_id" : ObjectId("4f65da9d8199c991088c9ca4"),
"games" : [
"Skyrim",
"Fallout New Vegas"
],
"name" : "jakub",
"surname" : "oboza"
}
db.people.update({_id: person._id}, { $push: {games: "World of Warcraft" } });
db.people.findOne({name: "jakub"});
{
"_id" : ObjectId("4f65da9d8199c991088c9ca4"),
"games" : [
"Skyrim",
"Fallout New Vegas",
"World of Warcraft"
],
"name" : "jakub",
"surname" : "oboza"
}
var question = db.people.update({_id: person._id}, { $pop: { games: 1} });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment