Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Get all. KIP ==> ques was to return ALL the restaurants names! just need .find()
- db.restaurants.find();
- // Limit and sort. KIP ==> periods off the db.restaurants to chain methods.
- db.restaurants.find().sort({name: 1}).limit(10);
- // Get by _id KIP ==> _id refers to the id made by mongo when you import your data
- var myId = db.restaurants.findOne({}, {_id: true})._id;
- db.restaurants.findOne({_id: myId}); //used to find specific id
- //brings back the Morris Park bakery b/c its the first one in the db.
- // Get by value ==> querying by specific value, kip => .find() from above
- db.restaurants.find({borough: "Queens"});
- // Count => targeting by specific value again, chaining off .count() method.
- db.restaurants.count(); //25359
- // Count by nested value -> returns # of restaurants in zip code 11206
- db.restaurants.find({'address.zipcode': ‘11206'}).count() //155
- // delete by id -> code below is ok, kip we’re still using js!!
- var myId = db.restaurants.findOne({}, {_id: true})._id;
- db.restaurants.removeOne({_id: myId});
- QQQ: delete did not work?!?
- // update a single document
- var myId = db.restaurants.findOne({}, {_id: true})._id;
- db.restaurants.updateOne(
- {_id: myId},
- {$set: {name: 'Bizz Bar Bang’}});
- QQQ: Mongo shell returns error!
- // update many documents ==> changes restaurants with the zip code 10035 to 10036
- db.restaurants.updateMany(
- {'address.zipcode': '10035'},
- {$set: {'address.zipcode': '10036'}});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement