use Aryan show dbs db.createCollection("Employee") //to create a table naemed employee show collections db.Employee.insert( { eid: 123, e_name: "Jack", salary: 25000, dept:"HR" }) db.Employee.insert( { eid: 124, e_name: "Smith", salary: 80000, dept:"HR" }) db.Employee.insert( { eid: 125, e_name: "Peter" }) db.Employee.insert( { eid: 126, e_name: "John", salary: 70000, dept:"Manager" }) db.Employee.insert( { eid: 127, e_name: "Smith", dept:"Account" }) db.Employee.insert( [ { }, //to insert many ]) db.Employee.find().pretty() // Q1. WAQ to find the details of those employees belong to HR dept db.Employee.find({dept:"HR"}) // Q2. Update the info of the employee from account dept to xyz db.Employee.update({dept:"Account"}, {$set:{dept:"xyz"}} ) db.Employee.find({dept:"xyz"}) // Q3. WAQ to insert two new employee info at same time // Q4. WAQ to find sum of salary for each employee db.Employee.aggregate([{$group:{_id:"salary", num_t:{$sum:1}}}]) use Aryan show dbs db.createCollection("Item") show collections db.Item.insert( [ { i_name: "Bread", price: 25, discount:2, brand:"A" }, { i_name: "Butter", price: 50, quantity:3, discount:8, brand:"A" }, { i_name: "Milk", price: 75, quantity:4, brand:"C" }, { i_name: "Rice", price: 185, discount:4, brand:"C" }, { i_name: "Wheat", price: 200, quantity:7, discount:6, brand:"B" }]) Q1. WAQ to find the sum of price for all items Q2. Find out the maximum value from the discount document Q3. Update the brand for items C to document Q4. Find the details of the items whose price greater than equals to 100 Q5. Remove only first entry where brand=A Q6. Find the details of those items where quantity is less than 5 Q7. List out all the item names from the given collection
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'})