Python MongoDB – insert_one Query

MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs. MongoDB is developed by MongoDB Inc. and initially released on 11 February 2009. It is written in C++, Go, JavaScript, Python languages. MongoDB offers high speed, high availability, and high scalability.

insert_one() 

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)

Parameters: 

  • ‘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 1: 



Sample Database:

 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing Mongoclient from pymongo
from pymongo import MongoClient 
  
# Making Connection
myclient = MongoClient("mongodb://localhost:27017/"
  
# database 
db = myclient["GFG"]
  
# Created or Switched to collection 
# names: GeeksForGeeks
collection = db["Student"]
  
# Creating Dictionary of records to be 
# inserted
record = { "_id": 5,
          "name": "Raju",
          "Roll No": "1005",
          "Branch": "CSE"}
  
  
  
# Inserting the record1 in the collection 
# by using collection.insert_one()
rec_id1 = collection.insert_one(record)

chevron_right


Output: 
 

Example 2: Inserting multiple values

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing Mongoclient from pymongo
from pymongo import MongoClient 
  
  
# Making Connection
myclient = MongoClient("mongodb://localhost:27017/"
  
# database 
db = myclient["GFG"]
  
# Created or Switched to collection 
# names: GeeksForGeeks
collection = db["Student"]
  
# Creating Dictionary of records to be 
# inserted
records = {
    "record1": { "_id": 6,
    "name": "Anshul",
    "Roll No": "1006",
    "Branch": "CSE"},
  
    "record2": { "_id": 7,
    "name": "Abhinav",
    "Roll No": "1007",
    "Branch": "ME"}
}
  
  
# Inserting the records in the collection 
# by using collection.insert_one()
for record in records.values():
    collection.insert_one(record)

chevron_right


Output: 




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.