Skip to content

Instantly share code, notes, and snippets.

@rfischer20
Created May 17, 2012 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfischer20/2719591 to your computer and use it in GitHub Desktop.
Save rfischer20/2719591 to your computer and use it in GitHub Desktop.
Examples for Intro to Mongodb
//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