Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Get All:
- db.restaurants.find({})
- Limit and sort: 1st 10 alphabetically
- db.restaurants.find({}, {name:1}).sort({name:1}).limit(10);
- Get by ID:
- db.restaurants.findOne({})._id;
- ObjectId("59074c7c057aaffaafb0da64")
- var documentId = ObjectId("59074c7c057aaffaafb0da64");
- db.restaurants.findOne({_id: documentId});
- Get by Value:
- db.restaurants.find({borough: "Queens"});
- Count # Documents
- db.restaurants.count();
- Count by nested value
- Write a command that gives the number of restaurants whose zip code value is '11206'
- db.restaurants.find({"address.zipcode": "11206"}).count();
- Delete by id
- Write a command that deletes a document from db.restaurants.
- db.restaurants.findOne({})._id;
- ObjectId("59074c7c057aaffaafb0da64")
- db.restaurants.deleteOne({_id: ObjectId("59074c7c057aaffaafb0da64")})
- Update a single document
- Write a command that sets the name property of a document with a specific _id to 'Bizz Bar Bang'.
- var objectId = db.restaurants.findOne({}, {_id: 1})._id
- db.restaurants.updateOne({_id: objectId}, {$set: {name: "Bizz Bar Bang"}})
- { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
- db.restaurants.findOne({_id: objectId});
- Update many documents: addresses with '10035' will now fall under the '10036' zip
- find all 10035 zip documents, update to 10036
- db.restaurants.updateMany({"address.zipcode": "10035"}, {$set: {"address.zipcode": 10036}})
- { "acknowledged" : true, "matchedCount" : 12, "modifiedCount" : 12 }
- > db.restaurants.find({"address.zipcode": "10036"}).count();
- 92
- > db.restaurants.find({"address.zipcode": "10035"}).count();
- 0
Add Comment
Please, Sign In to add comment