This is a method by which we can insert a single entry within the collection or the database in MongoDB. If the collection does not exist this method creates a new collection and insert the data into it. It takes dictionary as a parameter containing the name and value of each field in the document you want to insert in the collection.
This method returns an instance of class “~pymongo.results.InsertOneResult” which has a “_id” field that holds the id of the inserted document. If the document does not specify an “_id” field, then MongoDB will add the “_id” field and assign a unique object id for the document before inserting.
Syntax: collection.insert_one(document, bypass_document_validation=False, session=None)
- ‘document’: The document to insert. Must be a mutable mapping type. If the document does not have an _id field one will be added automatically.
- ‘bypass_document_validation’ (optional): If “True”, allows the write to opt-out of document level validation. Default is “False”.
- ‘session’ (optional): a class ‘~pymongo.client_session.ClientSession’.
Example 2: Inserting multiple values
- Difference Between insert(), insertOne(), and insertMany() in Pymongo
- Python MongoDB - insert_many Query
- Python MongoDB - Update_many Query
- Python MongoDB - find_one Query
- Python MongoDB - create_index Query
- Python MongoDB - find_one_and_delete Query
- Python MongoDB - Limit Query
- Python MongoDB - find_one_and_update Query
- Python MongoDB - drop_index Query
- Python MongoDB - Query
- Python MongoDB - find_one_and_replace Query
- MongoDB and Python
- Guide to Install MongoDB with Python | Windows
- MongoDB Python | Insert and Update Data
- MongoDB python | insert(), replace_one(), replace_many()
- MongoDB python | Delete Data and Drop Collection
- Python MongoDB - Find
- Python MongoDB - Sort
- Create a database in MongoDB using Python
- 3D Plotting sample Data from MongoDB Atlas Using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.