Created
May 17, 2012 15:17
-
-
Save rfischer20/2719591 to your computer and use it in GitHub Desktop.
Examples for Intro to Mongodb
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
//Check out the slides at https://speakerdeck.com/u/ryan_fischer/p/intro-to-mongodb | |
//create new document in collection, including an array and date | |
db.members.insert( {name:"Ryan", age: 29, skills: ["ruby", "mongodb"], created_at: new Date()}); | |
//find created user | |
db.members.find({name:"Ryan"}); | |
//update user | |
db.members.update( {name: "Ryan"}, {$set: {age: 28}}, 0, 0); | |
//push to array | |
db.members.update( {name: "Ryan"}, {$push: {skills: "rails"}}, 0, 0); | |
//pop from array | |
db.members.update( {name: "Ryan"}, {$pop: {skills: 1}}, 0, 0); | |
//pull from array | |
db.members.update( {name: "Ryan"}, {$pull: {skills: "ruby"}}, 0, 0); | |
//embed document | |
db.members.update( {name: "Ryan"}, {$set: {address: {addr1: "chicago ave.", city: "chicago", | |
state: "IL"}}}, 0, 0); | |
db.members.update({_id: me}, {$unset : {"class_ids":1}}, 0, 0); | |
//Create upsert while setting an increment for visitors | |
db.visitors.update({ip:'121.1.1'}, {$inc {visits: 1}}, 1, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment