// Design database for Zen class programme // inserting documents inside the users collection db.users.insertMany([ { "user-id": 1, "name": "user1", "email": "[email protected]", "mentor-id": 1, }, { "user-id": 2, "name": "user2", "email": "[email protected]", "mentor-id": 1, }, { "user-id": 3, "name": "user3", "email": "[email protected]", " mentor-id": 1, }, { "user-id": 4, "name": "user4", "email": "[email protected]", "mentor-id": 2, }, { "user-id": 5, "name": "user5", "email": "[email protected]", "mentor-id": 2, }, ]); // inserting documents inside the codekata collection db.codekata.insertMany([ { "user-id": 1, "no_of_problems_solved": 10, }, { "user-id": 2, "no_of_problems_solved": 20, }, { "user-id": 3, "no_of_problems_solved": 30, }, { "user-id": 4, "no_of_problems_solved": 40, }, { "user-id": 5, "no_of_problems_solved": 50, }, ]); // inserting documents inside the attendance collection db.attendance.insertMany([ { "user-id": 1, "topic_id": 1, "present": true, }, { "user-id": 2, "topic_id": 2, "present": true, }, { "user-id": 3, "topic_id": 3, "present": false, }, { "user-id": 4, "topic_id": 4, "present": false, }, { "user-id": 5, "topic_id": 5, "present": false, }, ]); // inserting documents inside the topics collection db.topics.insertMany([ { "topic_id": 1, "topic": "HTML", "topic_date": new Date("2021-10-01"), }, { "topic_id": 2, "topic": "CSS", "topic_date": new Date("2021-10-10"), }, { "topic_id": 3, "topic": "Javascript", "topic_date": new Date("2021-10-15"), }, { "topic_id": 4, "topic": "React", "topic_date": new Date("2021-10-20"), }, { "topic_id": 5, "topic": "NodeJs", "topic_date": new Date("2021-10-25"), }, ]); // inserting documents inside the tasks collection db.tasks.insertMany([ { "task_id": 1, 'topic_id': 1, "user-id": 1, "task": "HTML task", "due_date": new Date("2021-10-05"), "submitted": true, }, { "task_id": 2, 'topic_id': 2, "user-id": 2, "task": "CSS task", "due_date": new Date("2021-10-15"), "submitted": true, }, { "task_id": 3, "topic_id": 3, "user-id": 3, "task": "Javascript task", "due_date": new Date("2021-10-20"), "submitted": false, }, { "task_id": 4, "topic_id": 4, "user-id": 4, "task": "React task", "due_date": new Date("2021-10-25"), "submitted": false, }, { "task_id": 5, "topic_id": 5, "user-id": 5, "task": "Node task", "due_date": new Date("2021-10-30"), 'submitted': false, }, ]); // inserting documents inside the companydrives collection db.company_drives.insertMany([ { "user-id": 1, "drive_date": new Date("2021-10-05"), "company_name": "Google", }, { "user-id": 1, "drive_date": new Date("2021-10-10"), "company_name": "Amazon", }, { "user-id": 2, "drive_date": new Date("2021-10-20"), "company_name": "Walmart", }, { "user-id": 3, "drive_date": new Date("2021-10-15"), "company_name": "Zoho", }, { "user-id": 4, "drive_date": new Date("2021-10-30"), "company_name": "Dell", }, ]); // inserting documents inside the mentors collection db.mentors.insertMany([ { "mentor-id": 1, "mentor_name": "mentor1", "mentor_email": "[email protected]", }, { "mentor-id": 2, "mentor_name": "mentor2", "mentor_email": "[email protected]", }, { "mentor-id": 3, "mentor_name": "mentor3", "mentor_email": "[email protected]", }, { "mentor-id": 4, "mentor_name": "mentor4", "mentor_email": "[email protected]", }, { "mentor-id": 5, "mentor_name": "mentor5", "mentor_email": "[email protected]", }, ]);
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'})