Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python MongoDB - insert_many Query
Next article icon

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 was initially released on 11 February 2009. It is written in C++, Go, JavaScript, and Python languages. MongoDB offers high speed, high availability, and high scalability.

insert_one() Method

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 a 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, comment=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'.
  • 'comment'(optional): A user-provided comment to attach to this command.


Example 1: 

Sample database is as follows: 

Example

Python3
# importing Mongoclient from pymongofrompymongoimportMongoClient# Making Connectionmyclient=MongoClient("mongodb://localhost:27017/")# databasedb=myclient["GFG"]# Created or Switched to collection# names: GeeksForGeekscollection=db["Student"]# Creating Dictionary of records to be# insertedrecord={"_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)

Output: 

Example 2:Inserting multiple values

To insert multiple values, 2 Methods can be followed:

#1: Naive Method: Using for loop and insert_one

Python3
# importing Mongoclient from pymongofrompymongoimportMongoClient# Making Connectionmyclient=MongoClient("mongodb://localhost:27017/")# databasedb=myclient["GFG"]# Created or Switched to collection# names: GeeksForGeekscollection=db["Student"]# Creating Dictionary of records to be# insertedrecords={"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()forrecordinrecords.values():collection.insert_one(record)

Output: 

#2: Usinginsert_many method:This method can be used to insert multiple documents in a collection in MongoDB.

Theinsert_many method is explained briefly in the nexttutorial.


Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp