Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Examples for Intro to Mongodb
//Check out the slides at
//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
//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