Skip to content

Instantly share code, notes, and snippets.

@AkimaLunar
Last active June 3, 2017 04:05
Show Gist options
  • Save AkimaLunar/4d4d02347076fc58a86da873fe24a447 to your computer and use it in GitHub Desktop.
Save AkimaLunar/4d4d02347076fc58a86da873fe24a447 to your computer and use it in GitHub Desktop.
Mongo basics drills

Get all

db.restaurants.find()

Limit and sort

Find the command that makes the first 10 restaurants appear when db.restaurants is alphabetically sorted by the name property.

db.restaurants.find().limit(10).sort({name:1})

Get by _id

db.restaurants.find( { "_id": ObjectId("592f89aadbbf8af4e5f128a4") } ) db.restaurants.find( { "restaurant_id" : "50017969" } )

Get by value

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

Count

db.restaurants.count()

Count by nested value

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

Delete by id

db.restaurants.deleteOne({"_id": ObjectId("592f89aadbbf8af4e5f128a7")})

Update a single document

db.restaurants.updateOne( { "_id": ObjectId("592f89aadbbf8af4e5f128a4") }, { $set: {"name": "Dragon Beaux"}} )

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