Guest User

Untitled

a guest
Dec 15th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. Get All:
  2. db.restaurants.find({})
  3.  
  4. Limit and sort: 1st 10 alphabetically
  5. db.restaurants.find({}, {name:1}).sort({name:1}).limit(10);
  6.  
  7. Get by ID:
  8. db.restaurants.findOne({})._id;
  9. ObjectId("59074c7c057aaffaafb0da64")
  10. var documentId = ObjectId("59074c7c057aaffaafb0da64");
  11. db.restaurants.findOne({_id: documentId});
  12.  
  13. Get by Value:
  14. db.restaurants.find({borough: "Queens"});
  15.  
  16. Count # Documents
  17. db.restaurants.count();
  18.  
  19. Count by nested value
  20. Write a command that gives the number of restaurants whose zip code value is '11206'
  21. db.restaurants.find({"address.zipcode": "11206"}).count();
  22.  
  23. Delete by id
  24. Write a command that deletes a document from db.restaurants.
  25. db.restaurants.findOne({})._id;
  26. ObjectId("59074c7c057aaffaafb0da64")
  27. db.restaurants.deleteOne({_id: ObjectId("59074c7c057aaffaafb0da64")})
  28.  
  29. Update a single document
  30. Write a command that sets the name property of a document with a specific _id to 'Bizz Bar Bang'.
  31. var objectId = db.restaurants.findOne({}, {_id: 1})._id
  32. db.restaurants.updateOne({_id: objectId}, {$set: {name: "Bizz Bar Bang"}})
  33. { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
  34. db.restaurants.findOne({_id: objectId});
  35.  
  36. Update many documents: addresses with '10035' will now fall under the '10036' zip
  37. find all 10035 zip documents, update to 10036
  38. db.restaurants.updateMany({"address.zipcode": "10035"}, {$set: {"address.zipcode": 10036}})
  39. { "acknowledged" : true, "matchedCount" : 12, "modifiedCount" : 12 }
  40. > db.restaurants.find({"address.zipcode": "10036"}).count();
  41. 92
  42. > db.restaurants.find({"address.zipcode": "10035"}).count();
  43. 0
Add Comment
Please, Sign In to add comment