Create and Manage Database in MongoDB
Create and Manage Database in MongoDB
- Show /List out Databases available in MongoDB:
This command is used to list out various databases are available in MongoDB connection.
>show databases / dbs
- Open any specific Database:
This command is used to either open an existing database or to create a new one.
>use database_name
E.g: > use Employee
- Create a Collection:
This Command is useful to manually create collections
>db.createCollection(‘student’) OR
This below command is useful to create collection automatically and insert document within it.
>db.student.insert ( {“RollNo”: 1, “Name”: “Tejas”})
- Display the collections:
>use student
Student> show collections
This command is useful to display all the available collections from the current database student.
- Drop any Database:
>db.dropDatabase()
This command is useful to drop any database. But, user have to mention the name of the database as an argument. Otherwise, MongoDB drop test database automatically.
- Drop Collection:
>db.demo.drop()
This command is useful to drop the demo collection from the current database.
- Inserting a Document/Documents:
1. Insert one Document:
Syntax: db.collection.insert()
db.employee.insert({“Emp_id”: 2,”Emp_name” : “Raman”, “Emp_city”: “Jamshedpur”}); OR
db.employee.insertOne({“Emp_id”: 2,”Emp_name” : “Raman”, “Emp_city”: “Jamshedpur”});
2. Insert many Documents:
Syntax: db.collection.insertMany()
>db.employee.insertMany([{“Emp_id”: 3,”Emp_name”:”Tejas”, ”Emp_city”:”Jamnagar”},
{“Emp_id”: 4,”Emp_name”:”Suraj”, ”Emp_city”:”Ahmedabad”},
{“Emp_id”: 5,”Emp_name”:”Kosha”, ”Emp_city”:”Rajkot”}])
3. Insert Document using an Array:
//create an array to store the documents
var students = [ {RollNo:1, Sname:”Tanush”, City:”Jammu”, Result:”PASS”, Percentage:86 },
{RollNo:2, Sname:”Shankar”, City:”Ahmedabad”, Result:”PASS”, Percentage:80},
{RollNo:3, Sname:”Mahadev”, City:”Surat”, Result:”PASS”, Percentage:89 },
{RollNo:4, Sname:”Rajesh”, City:”Delhi”, Result:”PASS”, Percentage:56},
{RollNo:5, Sname:”Samay”, City:”Jaipur”, Result:”FAIL”, Percentage:33} ];
//insert documents (array).
db.student.insert(students);
- Updating a Document/Documents:
1. Update one Document:
Syntax: db.collection.updateOne()
>db.employee.update({“Emp_id”:
4},{$set:{“Emp_name”:”Vrinda”}})
2. Update Many Fields:
>db.employee.update({“Emp_id”: 1},{$set: {“Emp_name”:”Suman”, “Emp_city”: “Baroda”}})
3. Update Many Documents:
>db.student.updateMany({“Percentage”:{$lt:40}}, {$set:{“Result”: “Fail”}})
- Delete a Document/Documents:
1. Delete one Document:
To delete a single document from the collection that matches a specified filter.
>db.employee.remove({Emp_id: 1}) OR
>db.employee.deleteOne({Emp_id: 1})
2. Delete Many Documents:
To delete all the documents that match a deletion criteria.
>db.dmployee.deleteMany({City: “Surat”})
3. Delete all the Documents:
To delete all the documents from a collection, pass an empty filter document{} to this method.
>db.employee.deleteMany({})
- Managing Relationships between documents:
The Key decision in designing data modes for MongoDB applications revolves around the structure of documents and how the application represents relationships between data. Relationships in MongoDB represent how various documents are logically related to each other. Relationships can be molded via Embedded and Referenced approaches. Such relationships can be either one to one, one to many, Many to One or Many to Many.
1. Embedded Document:
Embedded documents capture relationships between data by storing related data in a single document structure. MongoDB documents make it possible to embed document structures in a filed or array within a document. These de-normalized data models allow applications to retrieve and manipulate related data in a single database operation.
E.g: One customer/ Employee can have multiple addresses like contact and/or permanent address. So, This example is also known as a one to Many relationship.
1. Employee Name Information: {“_id”:ObjectId(“52ffc33cd85242f436000001”),
“Sname”: “Tanmay”, “Course”: “MCA” }
1. Address documents:
{“_id”:ObjectId(“52ffc4a5d85242602e000000”), “Building”: “22 A, Sudha Apt”, “pincode”: 123456, “City”: “Ahmedabad”, “State”: “Gujarat” }
If the address data is frequently retrieved with the name information, then with referencing, your application needs to issue multiple queries to resolve the reference. The better data model would be embed the address data in the main data, as in the following document:
1. One to One Relationship:
With the embedded data model, your application can retrieve the complete patron information with one query.
E.g: db.student.insert( {“_id”: 1, “Sname”: “Tanmay”,”Course”: “MCA”, Address:
{Street: “5-ShivKrupa Sociecty”, City: “Jamnagar”, Pincode: 12345, State:
“Gujarat”}})
2.One to Many Relationship:
If your application frequently retrieves the address data with the name information, then your application needs to issue multiple queries to resolve the references. A more optimal schema would be to embed the address data entities in the name data, as in the following document:
E.g: {RollNo: 1, Name:”Tanush”, Address:[{
Street:”5-Shivkrupa society”,
City: “Jammu”, Zip: 12345, State: “Jammu”},
{Street: “413-Anandi Township”,
City:”Surat”, Zip: 23456, State: “Gujarat”}]
2. Reference Document:
References store the relationships between data by including links or reference from one document to another. Applications can resolve these references to access the related data.