Defining, Creating and Dropping a MongoDB collection
MongoDB is an open-source, cross-platform document-based database.
Document: It is the basic unit of MongoDB and a single record inside a collection is known as a document.
It is a JSON object which has the data in the form of key-value pairs.
Collection: It is the grouping of documents. A collection belongs to the single database inside MongoDB.
Collection contains the data in the form of document just like the table of RDBMS which contains the data in the form of rows and columns.
-
Creating a mongodb collection:
-
STEP 1: First run the following command into your mongo shell or command prompt
mongo
-
STEP 2: Check all the existing databases by using the following command:
show dbs
-
STEP 3: Creating a database
Here we are creating a database named as- geeksforgeeksuse geeksforgeeks
If the database geeksforgeeks is present it will switch to it, else it will create a new database name as geeksforgeeks and then switch to it as shown below.
As a collection belongs to the single database so first we will have to create a database for creating a collection:
-
Inserting documents in the collection
- Inserting only one document at a time
db.collection.insertOne(document)
- Inserting more than one document at a time
db.collection.insertMany([ document_1, document_2..... ])
Editing a collection:
db.collection.update(query, update) query: An object for finding the document Example: {"name":"shilpi"} update: An object for updating the document with set command Example: {$set:{"age":"23"}}
- To edit a mongodb collection update function is used
- Apply the query for finding the document which you want to update.
In our collection we want to update collection having name as shilpi so query is:{"name":"shilpi"}
- Using the set command to modify the Field Name
- Choose which Field Name you want to modify and then enter the new value accordingly into the query.
In our collection we want to edit age value to 23 for name shilpi:{$set:{"age":"23"}}
Dropping the collection:
db.collection.drop()
It will return true if collection is deleted successfully.
Please Login to comment...