To insert data in MongoDB Database, first we have to create a collection ( like table) and document data in it. To do this, lets start MongoDB client in terminal. A collection is like a table in MongoDB. In a MongoDB database, we can create n numbers of collections. Inside
collections, we have documents ( rows ) to store data in key-value pair. In order to connect to MongoDB database, we have to start
MongoDB shell first. admin 0.000GB cars 0.000GB config 0.000GB local 0.000GB switched to db cars insertOne method is used to insert single document in collection. insertOne was introduced in MongoDB 3.2. { "_id": ObjectId("5eb44282ea2adece905a24b0"), "name": "swift", "type": "hatchback" }
This is the final output in MongoDB Compass. Primary KeyThe ObjectId stored in "_id" is the primary key. The primary key is always unique for all documents. It is always recommended to use natural primary keys in MongoDB. The primary key generated by MongoDB use 12 bytes of storage. Which means it can store 24 digits, 2 digits per byte. Lets try to understand primary keys pattern.
First four bytes are timestamp in seconds since EPOCH ( 1-jan-1947 00:00). Next three bytes are for machine. Next two for processid and last three bytes are increment. ObjectId("5eb451b9f2e0e1f61b7cfe07") id ISODate("2020-05-07T18:21:45Z") id.getTimestamp() insertManyinsertMany method introduced in 3.2 is used to insert multiple documents in collections. The argument in insertMany is an array of objects. See example { "acknowledged" : true, "insertedIds" : [ ObjectId("5eb5378791fc73f290f38251"), ObjectId("5eb5378791fc73f290f38252") ] }
The insert method is used to insert single or multiple documents in a collections . The is the most preferred method to insert data in mongoDB. The argument in insert method can be object ( single document ) or an array ( multiple documents ). Insert SingleWriteResult({ "nInserted" : 1 })
Insert MultipleBulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
Home » MongoDB » MongoDB Insert Document in A Collection Tutorial In this MongoDB tutorial, we will look at how to insert document into a collection. Please note if you insert a record in a non-existing collection, then MongoDB will create a new collection for you by default. MongoDB offers various methods to insert document in a database. In this tutorial we will talk about 3 of the mostly used MongoDB methods to insert records into collection. Switching to MongoDB DatabaseFirst, we need to access the MongoDB shell and switching to MongoDB database to insert the record in a collection. Now, we will create a new Database using the use command. If the database is non-existing, then the use command will create a new database in MongoDB. Run the command:
Access your MongoDB database, in my case
The MongoDB Insert() Method ExampleThe insert() method helps in inserting single or multiple documents in a collection in MongoDB. It takes a document or array of documents as a 1st argument, and as a second argument, we pass some settings in it as mentioned below in the syntax.
Understand the Parameters
Visit MongoDB site to know more about insert method. Insert Document without _id FieldIn this example, we will be creating a cars collection and populating some records into it.
Insert Method Output:
As you can see, In the example above we have created a cars collection and added some data into it. Since the cars collection wasn’t exist so MongoDB created a new cars collection, added the records into the document and assigned a new _id to the object. MongoDB Insert Multiple Documents in CollectionIn this example, we will insert multiple documents using the insert method. We can supply an array of document inside the Enter command and hit enter:
Once you hit enter in the terminal following message will be displayed:
Following data has been inserted in MongoDB collection:
Using insertOne() MethodIn MongoDB we can use the The insertOne() Syntax
Introduction to insertOne() Parameter
Click here to know more about insertOne() method .
Following message will be shown on the terminal:
You can check out newly cerated employee collection using MongoDB’s db.collectionName.find().pretty() method in the terminal:
Using insertMany() Method to Insert Multiple DocumentsIn the final step of this MongoDB tutorial, we are going to use insertMany() method to insert multiple records in a collection. Introduction to insertMany Syntax
Understand The Parameters
To know more about Insert Multiple Items using insertMany() MethodLet us insert multiple items in a
If successfully inserted and created collection then below message will be shown on the terminal:
Check out the
Recommended Posts:How do I create a collection in MongoDB?MongoDB creates collections automatically when you insert some documents. For example: Insert a document named seomount into a collection named SSSIT. The operation will create the collection if the collection does not currently exist. If you want to see the inserted document, use the find() command.
Which command is used to insert data into collection in MongoDB?The insert() Method
To insert data into MongoDB collection, you need to use MongoDB's insert() or save() method.
How do I import data from one collection to another in MongoDB?In MongoDB, copyTo() method is used to copies all the documents from one collection(Source collection) to another collection(Target collection) using server-side JavaScript and if that other collection(Target collection) is not present then MongoDB creates a new collection with that name.
How do I create a collection and document in MongoDB?In MongoDB, you don't need to create collection. MongoDB creates collection automatically, when you insert some document.
|