Video Transcript (English)
Hello!
In this video, you’ll learn how to insert documents into a MongoDB database by using the PyMongo driver.
There are two methods that we can use to insert documents into MongoDB: insert_one() is used to insert a single document into a MongoDB collection. insert_many() is used to insert multiple documents.
First, let’s see insert_one() in action.
We’re working with a database named `bank` that contains an `accounts` collection.
We want to insert this document, which contains data on a new checking account, into the `accounts` collection.
This document has a number of fields that contain information about the account, such as the account holder, account id, account type, balance, and the date the account data was last updated.
Notice that we have not included an _id field.
MongoDB documents are assigned an _id field that uniquely identifies each document in a collection after the document is inserted. We can think of the _id field as being equivalent to a primary key. If we don’t provide a value for the _id field, the driver will generate the _id value for us.
In this document, we’ve omitted the _id field so MongoDB will create it for us automatically.
We’ve already started a Python file and made a connection to our Atlas cluster with MongoClient.
Before we can insert our document, we need to create references to the database and collection that we want to operate on.
Remember, we’re using the `bank` database and `accounts` collection.
We create a variable called db with a reference to the `bank` database.
Then we create an accounts_collection variable with a reference to the accounts collection within the bank database.
We assign the document that we want to insert to a variable named ‘new_account’.
Now we’re ready to write the expression that inserts our document.
To do this, we call the insert_one() method on the accounts_collection object.
insert_one() has one required argument, which is the document we want to insert.
We pass in our document, which is stored in the new_account variable.
The insert_one() method returns a result, which gives us access to the _id of the inserted document. This allows us to print the _id to the terminal.
We assign the result of the insert operation to a variable named `result.`
We get a reference to the inserted_id and write a statement to print it out.
Finally, we close our connection by calling the close() method on the client.
Now let’s run the file. The _id value that MongoDB created for us is printed out.
Next, we’ll use the insert_many() method to insert multiple documents into a collection.
Let’s say we have two more accounts that we want to insert into the accounts collection, a checking account and a savings account
We’ve started a new Python file, connected to our Atlas cluster with MongoClient, and created references to the database and collection.
Because insert_many() takes an iterable of documents to insert, we put the accounts into a list.
We assign the list to a variable called ‘new_accounts’.
Now we’re ready to write the expression to insert the documents.
We call the insert_many() method on the accounts_collection object, and we pass in the documents, which are stored in the `new_accounts` variable.
The insert_many() method returns a result, which gives us access to the _ids of the inserted documents.
We can then print out the number of documents inserted and their _id values.
We assign the result of the insert operation to a variable named `result.`
We get the inserted_id values from the result object.
Then we write statements to print out the number of documents inserted and their _id values.
Finally, we close the database connection.
Now let’s run the file. The output confirms that our two documents were inserted, as well as their `_id` values.
Well done!
You now know how to use the insert_one() and insert_many() methods to insert a single document and multiple documents into a MongoDB collection.