Created
March 18, 2012 12:59
-
-
Save JakubOboza/2071805 to your computer and use it in GitHub Desktop.
mongodb push / pushall / pop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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