Skip to content

Instantly share code, notes, and snippets.

@johnayoung
Last active January 7, 2019 17:16
Show Gist options
  • Save johnayoung/f0c69e776a098d63d19a46f75059fee9 to your computer and use it in GitHub Desktop.
Save johnayoung/f0c69e776a098d63d19a46f75059fee9 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)

Get by id

const myId = db.restaurants.findOne({}, {_id: 1})._id
db.restaurants.findOne({_id: myId})

Get by Value

db.restaurants.find({borough: "Queens"})

Count

db.restaurants.find({'address.zipcode': "11206"}).count()

Delete by id

const delId = db.restaurants.findOne({}, {_id: 1})._id
db.restaurants.deleteOne({_id: delId})

Update a single document

const updId = db.restaurants.findOne({}, {_id: 1})._id
db.restaurants.updateOne(
  {_id: updId},
  {$set: {name: "Foo Bar Bizz 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