Skip to content

Instantly share code, notes, and snippets.

@theGaffe
Created October 9, 2018 23:05
Show Gist options
  • Save theGaffe/0ab59e84efe7ce0162b6967d61c23ce8 to your computer and use it in GitHub Desktop.
Save theGaffe/0ab59e84efe7ce0162b6967d61c23ce8 to your computer and use it in GitHub Desktop.
Mongo basics drills
//Get All
db.restaurants.find()
//Limit and sort
db.restaurants.find().sort({name: 1}).limit(10)
//Get by _id
var id = db.restaurants.findOne()._id
db.restaurants.findOne({_id: id}})
//Get by value
db.restaurants.find({borough: "Queens"})
//Count
db.restaurants.count()
//Count by nestest value
db.restaurants.find({"address.zipcode": "11206"}).count()
//Delete by id
var id = db.restaurants.findOne()._id
db.restaurants.deleteOne({_id: id})
//Update a single document
var id = db.restaurants.findOne()._id
db.restaurants.updateOne( { _id: id}, {$set: {"name": "Bizz Bar Bang"}});
//Update many documents
db.restaurants.updateMany({ "address.zipcode": "10035" }, { $set: {"address.zipcode": "10036"} } );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment