Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. // Get all. KIP ==> ques was to return ALL the restaurants names! just need .find()
  2. db.restaurants.find();
  3.  
  4. // Limit and sort. KIP ==> periods off the db.restaurants to chain methods.
  5. db.restaurants.find().sort({name: 1}).limit(10);
  6.  
  7. // Get by _id KIP ==> _id refers to the id made by mongo when you import your data
  8. var myId = db.restaurants.findOne({}, {_id: true})._id;
  9. db.restaurants.findOne({_id: myId}); //used to find specific id
  10. //brings back the Morris Park bakery b/c its the first one in the db.
  11.  
  12. // Get by value ==> querying by specific value, kip => .find() from above
  13. db.restaurants.find({borough: "Queens"});
  14.  
  15. // Count => targeting by specific value again, chaining off .count() method.
  16. db.restaurants.count(); //25359
  17.  
  18. // Count by nested value -> returns # of restaurants in zip code 11206
  19. db.restaurants.find({'address.zipcode': ‘11206'}).count() //155
  20.  
  21. // delete by id -> code below is ok, kip we’re still using js!!
  22. var myId = db.restaurants.findOne({}, {_id: true})._id;
  23. db.restaurants.removeOne({_id: myId});
  24. QQQ: delete did not work?!?
  25.  
  26. // update a single document
  27. var myId = db.restaurants.findOne({}, {_id: true})._id;
  28. db.restaurants.updateOne(
  29. {_id: myId},
  30. {$set: {name: 'Bizz Bar Bang’}});
  31. QQQ: Mongo shell returns error!
  32.  
  33. // update many documents ==> changes restaurants with the zip code 10035 to 10036
  34. db.restaurants.updateMany(
  35. {'address.zipcode': '10035'},
  36. {$set: {'address.zipcode': '10036'}});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement