Skip to content

Instantly share code, notes, and snippets.

@iancover
Last active February 5, 2022 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iancover/ee1a204ecfef17d865d2d33d3d069ce8 to your computer and use it in GitHub Desktop.
Save iancover/ee1a204ecfef17d865d2d33d3d069ce8 to your computer and use it in GitHub Desktop.
Mongo shell commands
# GET ALL
# Command that retrieves all restaurants
$ db.restaurants.find();
# LIMIT AND SORT
# Find the command that makes the first 10 restaurants appear alphabetically by 'name'
$ db.restaurants.
find().
sort({name:1}).
limit(10);
# GET BY ID
# Retrieve a single restaurant by id
$ db.restaurants.find({_id: ObjectId("")});
# GET BY VALUE
# Command that gets restaurants in Queens
$ db.restaurants.find({borough: "Queens"});
# COUNT
# Command that give number of docs in 'restaurants'
$ db.restaurants.count();
# COUNT BY NESTED VALUE
# Command that gives number of restaurants whose zip code value is '11206'
$ db.restaurants.count({'address.zipcode':'11206'});
# DELETE BY ID
# Command that deletes document by its Id
$ db.restaurants.deleteOne({_id:ObjectId("59074c7c057aaffaafb116cc")});
# UPDATE SINGLE DOC
# Command that sets the name property of a specific doc by id to 'Bizz Bar Bang'
$ db.restaurants.updateOne({_id:ObjectId("59074c7c057aaffaafb11c28")},{$set:{name:"Biz Bar Bang"}});
# UPDATE MANY DOCUMENTS
# Command that merges zipcode '10035' with '10036'
$ db.restaurants.updateMany({"address.zipcode":"10035"},{$set:{"address.zipcode":"10036"}});
@rodrigoalvesvieira
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment