Article image Inserting documents into MongoDB: Inserting simple documents into MongoDB

8.7. Inserting documents into MongoDB: Inserting simple documents into MongoDB

Page 15 | Listen in audio

When working with MongoDB, one of the most fundamental operations you need to understand is how to insert documents into the database. Inserting documents is the equivalent of adding records to a relational database. In MongoDB, documents represent a record of data. In this chapter, we will cover how to insert simple documents into MongoDB.

Firstly, it is important to understand that MongoDB is a document-oriented database. This means that instead of storing your data in tables as would be done in a relational database, MongoDB stores data in BSON type structures (a binary representation of JSON with additional data types). Each document has a set of field-value pairs. Fields can contain other documents, arrays and document arrays.

Insert operation is performed using insert() or insertOne() and insertMany() method in MongoDB. The insert() method was the main document insertion method in MongoDB before version 3.2. From version 3.2, insertOne() and insertMany() were introduced to insert a single document and multiple documents respectively.

To insert a simple document into MongoDB, you will first need to select the collection you want to insert the document into. A collection in MongoDB is a group of documents and is equivalent to a table in a relational database. If the collection doesn't exist, MongoDB will create it for you.

Suppose you want to insert the following document into a collection called 'students':

{
  "name": "John Doe",
  "age": 22,
  "subjects": ["Math", "Science", "English"]
}

You can do this using the insertOne() method as follows:

db.students.insertOne({
  "name": "John Doe",
  "age": 22,
  "subjects": ["Math", "Science", "English"]
});

The insertOne() method inserts a new document into the specified collection. The document to be inserted is passed as an argument to the insertOne() method.

After the document is inserted, MongoDB returns a result document that contains the ID of the inserted document. The document ID is a special field called '_id' that is automatically added by MongoDB if you don't provide one. The '_id' field must be unique within the collection.

If you want to insert multiple documents at once, you can use the insertMany() method. The insertMany() method accepts an array of documents to be inserted. Here is an example:

db.students.insertMany([
  {
    "name": "Jane Doe",
    "age": 20,
    "subjects": ["History", "Math", "English"]
  },
  {
    "name": "Bob Smith",
    "age": 22,
    "subjects": ["Biology", "Science", "Physics"]
  }
]);

Just like the insertOne() method, the insertMany() method also returns a result document that contains the IDs of the inserted documents.

It is important to note that if an error occurs while inserting documents using the insertMany() method, MongoDB will abort the operation and none of the remaining documents in the array will be inserted. If you want MongoDB to insert as many documents as possible regardless of errors, you can pass an options object with the 'ordered' property set to false. This will cause MongoDB to insert documents in the order they appear in the array, but will not stop the operation if an error occurs.

In short, inserting documents into MongoDB is a simple but fundamental operation that you need to understand to work effectively with this database. Whether inserting a single document with insertOne() or multiple documents with insertMany(), the process is straightforward and flexible, allowing you to structure your data in any way that makes the most sense. for your specific needs.

Now answer the exercise about the content:

Which of the following methods is used to insert a single document into MongoDB?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Inserting documents into MongoDB: Inserting complex documents into MongoDB

Next page of the Free Ebook:

16Inserting documents into MongoDB: Inserting complex documents into MongoDB

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text