One of the most fundamental tasks when working with any database is entering data into it. In MongoDB, data is stored as documents, which are data structures similar to JSON (JavaScript Object Notation). In this chapter, we will discuss how to insert documents into a MongoDB collection.
Before we begin, it's important to understand that in MongoDB, a database is not created until data is stored in it. Therefore, the first time we insert a document into a collection, MongoDB will create both the database and the collection for us.
Inserting a single document
To insert a single document into a collection, we use the insertOne()
method. This method creates a new document if the fields in the document do not exist in the collection. If the document contains an "_id" field, the method will check the collection to see if a document with the same ID already exists. If the document exists, the method will return an error.
db.collection('collectionName').insertOne( { item: "item1", qty: 10 }, function(err, result) { console.log(result); } );
In this example, we are inserting a document with two fields, "item" and "qty", into the collection called 'collectionName'. If the insertion is successful, the method returns a result object that contains information about the operation.
Inserting multiple documents
To insert multiple documents into a collection at once, we use the insertMany()
method. This method accepts an array of documents to be inserted into the collection.
db.collection('collectionName').insertMany( [ { item: "item1", qty: 10 }, { item: "item2", qty: 20 }, { item: "item3", qty: 30 } ], function(err, result) { console.log(result); } );
In this example, we are inserting three documents into the 'collectionName' collection. Again, if the insertion is successful, the method returns a result object that contains information about the operation.
Working with the _id field
In MongoDB, each document must have a unique "_id" field that acts as the primary key for the document. If a document being inserted does not contain an "_id" field, MongoDB will add one for us. The value of the "_id" field is an ObjectId object that is automatically generated by MongoDB.
We can also provide our own value for the "_id" field when we insert a document. However, we must ensure that the value is unique in the collection.
db.collection('collectionName').insertOne( { _id: "myId", item: "item1", qty: 10 }, function(err, result) { console.log(result); } );
In this example, we are providing our own value for the "_id" field when we insert a document. If a document with the same ID already exists in the collection, the insert operation fails.
Conclusion
Inserting documents into a collection is a basic but essential task when working with MongoDB. Through the use of the insertOne()
and insertMany()
methods, we can insert a single document or multiple documents into a collection at once. We also have the flexibility to provide our own value for a document's "_id" field, as long as it is unique in the collection.
Understanding how to insert documents is an important step to working effectively with MongoDB. In the next chapter, we will discuss how to query documents in a MongoDB collection.