Create Operations
The create or insert operations are used to insert or add new documents in the collection. If a collection does not exist, then it will create a new collection in the database.
You can perform, create operations using the following methods provided by the MongoDB:
Method | Description |
---|---|
db.collection.insertOne() | It is used to insert a single document in the collection. |
db.collection.insertMany() | It is used to insert multiple documents in the collection. |
db.createCollection() | It is used to create an empty collection. |
Create Operations Example
Let’s look at some examples of the Create operation from CRUD in MongoDB.
Example 1: In this example, we are inserting details of a single student in the form of document in the student collection using db.collection.insertOne() method. Example 2: In this example, we are inserting details of the multiple students in the form of documents in the student collection using db.collection.insertMany() method.
MongoDB CRUD Operations
CRUD Operations (Create, Read, Update, and Delete) are the basic set of operations that allow users to interact with the MongoDB server.
As we know, to use MongoDB we need to interact with the MongoDB server to perform certain operations like entering new data into the application, updating data into the application, deleting data from the application, and reading the application data.
In this article, we will learn all 4 major operations– CREATE, READ, UPDATE, and DELETE that form the CRUD operations in MongoDB.