Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Convert PyMongo Cursor to JSON
Next article icon

Prerequisites:MongoDB and Python,Working With JSON Data in PythonMongoDB 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.JSON stands for JavaScript Object Notation. It is an open standard file format, and data interchange format with an extension ".json", that uses human-readable text to store and transmit data objects consisting of attribute-value pairs and array data types.

Importing JSON file in MongoDB

To import a JSON file in MongoDB we have to first load or open the JSON file after that we can easily insert that file into the database or the collection. To load a JSON file we have to first import json in our code after that we can open the JSON file. When our file gets loaded or opened we can easily insert it into the collection and operate on that file. Let's see the example for better understanding.Example :Sample JSON used:python-mongodb-json 

Python3
importjsonfrompymongoimportMongoClient# Making Connectionmyclient=MongoClient("mongodb://localhost:27017/")# databasedb=myclient["GFG"]# Created or Switched to collection# names: GeeksForGeeksCollection=db["data"]# Loading or Opening the json filewithopen('data.json')asfile:file_data=json.load(file)# Inserting the loaded data in the Collection# if JSON contains data more than one entry# insert_many is used else insert_one is usedifisinstance(file_data,list):Collection.insert_many(file_data)else:Collection.insert_one(file_data)

Output:python-json-to-mongodb


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