Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node U2L1 Drills
// Get all
db.restaurants.find();
// Limit and sort
db.restaurants.
find().
sort({name: 1}).
limit(10);
// Get by _id
var myId = db.restaurants.findOne({}, {_id: true})._id;
db.restaurants.findOne({_id: myId});
// Get by value
db.restaurants.find({borough: "Queens"});
// Count
db.restaurants.count();
// Count by nested value
db.restaurants.find({'address.zipcode': '11206'}).count()
// delete by id
var myId = db.restaurants.findOne({}, {_id: true})._id;
db.restaurants.removeOne({_id: myId});
// update a single document
var myId = db.restaurants.findOne({}, {_id: true})._id;
db.restaurants.updateOne(
{_id: myId},
{$set: {name: 'Bizz Bar Bang'}});
// update many documents
db.restaurants.updateMany(
{'address.zipcode': '10035'},
{$set: {'address.zipcode': '10036'}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment