//2a. List all restaurants' name that the restaurants are cuisine of Italian, //locate in zip 10036 and grade is A, sort data by name in ascending order db.restaurant.find({},{"name":1,"cuisine":"Italian","address.zipcode" :"10036","_id":0, "grade":"A"}).pretty() //2b. How many American Cuisine in Borough of Brooklyn? // Answer : 1273 db.restaurant.count({"cuisine":"American", "borough":"Brooklyn"}) //2c. ) Create a restaurant: //address //building: baker center //street: 2000 clayton state //zipcode: 30260 //borough: morrow //cuisine: mongodb //name: A Cup of Mongo db.restaurant.insert({"address":1, "address.building":"baker center", "address.street":"2000 clayton state", "address.zipcode":"30260", "borough":"morrow", "cuisine":"mongodb", "name":"A Cup of Mongo"}) //2d. modify the restaurant name from "Beija-Flor" to "King of Beija" //name :"Beija-Flor" //restaurant_id :"50005087" db.restaurant.update( {restaurant_id :"50005087" }, {$set:{"name":"King of Beija"}} ) //2e.Delete the restaurant named "Buffet 58" with restaurant_id "50009900" db.restaurant.remove( {restaurant_id :"50009900" } )
Write, Run & Share MongoDB queries online using OneCompiler's MongoDB online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for MongoDB. Getting started with the OneCompiler's MongoDB editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'MongoDB' and start writing queries to learn and test online without worrying about tedious process of installation.
MongoDB is a cross platform document oriented NoSQL database.
db.collection.insert()
: Using insert
you can either insert one document or array of documentsdb.employees.insert( {empId: 3, name: 'Ava', dept: 'Sales' });
db.collection.insertOne()
: Inserts one documentdb.employees.insertOne( {empId: 4, name: 'Nick', dept: 'Accounting' });
db.collection.insertMany
: Inserts multiple documentsdb.employees.insertMany([
{empId: 1, name: 'Clark', dept: 'Sales' },
{empId: 2, name: 'Dave', dept: 'Accounting' }
]);
db.collection.update()
: Updates one or more than one document(s) in collection based on matching document and based on multi
optiondb.employees.update(
{empId: 3 },
{ $set: { region: "Asia" } }
);
db.collection.updateOne()
: Updates a single document in collection based on matching documentdb.employees.updateOne(
{empId: 2 },
{ $set: { region: "Asia" } }
);
db.collection.updateMany()
: Updates multiple documents in collection based on the condition.db.employees.updateMany(
{ dept: 'Sales'},
{ $set: { region: "US" } }
);
db.collection.deleteOne(<filter>, <options>)
: Deletes a Single document from collectiondb.employees.deleteOne({ empId: 1})
db.collection.deleteMany(<filter>, <options>)
: Deletes all documents with matching filterdb.employees.deleteMany({ dept: 'Sales'})