Guest User

Untitled

a guest
Jan 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. // Get all
  2. // Find the command that retrieves all restaurants.
  3. db.restaurants.find()
  4.  
  5. // Limit and sort
  6. // Find the command that makes the first 10 restaurants appear when db.restaurants is alphabetically sorted by the name property.
  7. db.restaurants.find().sort(
  8. {name: 1} ).limit(10)
  9.  
  10. // Get by _id
  11. // Retrieve a single restaurant by _id from the restaurants collection.
  12. // This means you'll first need to get the _id for one of the restaurants imported into the database.
  13. db.restaurants.find(
  14. { _id: db.restaurants.findOne()._id } )
  15.  
  16. // Get by value
  17. // Write a command that gets all restaurants from the borough of "Queens".
  18. db.restaurants.find({"borough": "Queens"})
  19.  
  20. // Count
  21. // Write a command that gives the number of documents in db.restaurants.
  22. db.restaurants.count()
  23.  
  24. // Count by nested value
  25. // Write a command that gives the number of restaurants whose zip code value is '11206'.
  26. // Note that this property is at document.address.zipcode,
  27. // so you'll need to use dot notation to query on the nested zip code property.
  28. db.restaurants.find(
  29. {'address.zipcode': '11206'}).count()
  30.  
  31. // Update a single document
  32. // Write a command that sets the name property of a document with a specific _id to 'Bizz Bar Bang'.
  33. // Make sure that you're not replacing the existing document, but instead updating only the name property.
  34. db.restaurants.update(
  35. { _id: db.restaurants.findOne()._id },
  36. { 'name': 'Bizz Bar Bang' });
  37.  
  38. // Update many documents
  39. // Uh oh, two zip codes are being merged! The '10035' zip code is being retired,
  40. // and addresses with '10035' will now fall under the '10036' zip code. Write a command that updates values accordingly.
  41. db.restaurants.updateMany(
  42. {'address.zipcode': '10035'},
  43. {$set: {'address.zipcode': '10036'}});
Add Comment
Please, Sign In to add comment