Skip to content

Instantly share code, notes, and snippets.

@821jieun
Created March 27, 2018 19:05
Show Gist options
  • Save 821jieun/6f47429ae3d4676fa2a591407df185e6 to your computer and use it in GitHub Desktop.
Save 821jieun/6f47429ae3d4676fa2a591407df185e6 to your computer and use it in GitHub Desktop.
//get all
db.restaurants.find({});
//limit and sort
db.restaurants
.find()
.sort({name:1})
.limit(10);
//find by id
var myId = db.restaurants.findOne({}, {_id: true})._id;
db.restaurants.findOne({_id: myId});
//get by value
db.restaurants.find({
borough: "Queens"
});
//count
db.restaurants
.count();
//count by nested value
db.restaurants
.find({
"address.zipcode": "11206"
})
.count();
//delete by id
var myId = db.restaurants.findOne({}, {_id: true})._id;
db.restaurants.deleteOne({_id: myId});
//update a single document
var myId = db.restaurants.findOne({}, {_id: true})._id;
db.restaurants
.updateOne({
{ id : myId },
{$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