[Note] Operate MongoDB with Python

I often forget it together with postgreSQL, so I will write it down. Please refer to How to use pymongo for detailed operations such as specifying conditions.

from pymongo import MongoClient


mongo_server = 'localhost'
mongo_port = '27017'
mongo_database_name = 'database'
mongo_collection_name = 'collection'

def displ
ay_data(collection, mongo_filter=None):
    data = collection.find(filter=mongo_filter)
    for elem in data: 
        print(elem)
      
 
def insert_data(collection, data):
    collection.insert_one(data)
    

if __name__ =='__main__':
    mongoclient = MongoClient('mongodb://' + mongo_server + ':' + mongo_port + '/')
    db = mongoclient[mongo_database_name]
    collection = db[mongo_collection_name]
    #Data entry
    for i in range(10):
        data = {'count' :i}
        insert_data(collection, data)
    #Data display
    display_data(collection)

Execution result

{'_id': ObjectId('5e168f7828ba5616d6873582'), 'count': 0}
{'_id': ObjectId('5e168f7828ba5616d6873583'), 'count': 1}
{'_id': ObjectId('5e168f7828ba5616d6873584'), 'count': 2}
{'_id': ObjectId('5e168f7828ba5616d6873585'), 'count': 3}
{'_id': ObjectId('5e168f7828ba5616d6873586'), 'count': 4}
{'_id': ObjectId('5e168f7828ba5616d6873587'), 'count': 5}
{'_id': ObjectId('5e168f7828ba5616d6873588'), 'count': 6}
{'_id': ObjectId('5e168f7828ba5616d6873589'), 'count': 7}
{'_id': ObjectId('5e168f7828ba5616d687358a'), 'count': 8}
{'_id': ObjectId('5e168f7828ba5616d687358b'), 'count': 9}

Recommended Posts

[Note] Operate MongoDB with Python
Operate Kinesis with Python
Operate Blender with Python
Operate Excel with Python (1)
Operate Excel with Python (2)
Operate TwitterBot with Lambda, Python
[Python] [SQLite3] Operate SQLite with Python (Basic)
Note: Python
Python note
Operate a receipt printer with python
(Note) Be careful with python argparse
[Note] Hello world output with python
Operate ECHONET Lite appliances with Python
Note: Python Decorator
FizzBuzz with Python3
Python programming note
Scraping with Python
[Python] Learning Note 1
Statistics with python
Scraping with Python
Python with Go
Note when creating an environment with python
Operate smartlife power supply with python (de-IFTTT)
Python study note_004
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
[Personal note] Web page scraping with python3
python starts with ()
[GCP] Operate Google Cloud Storage with Python
Trying to handle SQLite3 with Python [Note]
with syntax (Python)
Python study note_003
[Pyto] Operate iPhone Taptic Engine with Python
Bingo with python
Zundokokiyoshi with python
[Note] openCV + python
[Python] Automatically operate the browser with Selenium
Operate home appliances with Python and IRKit
Python beginner's note
Excel with Python
Microcomputer with Python
Cast with python
[Note] Export the html of the site with python.
[Note] Create a one-line timezone class with python
Note for formatting numbers with python format function
Homebrew Vim doesn't start with Python 3.8 error Note
Install MongoDB on Ubuntu 16.04 and operate via python
Python script to get note information with REAPER
Settings for getting started with MongoDB in python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
mongodb access with pymongo
Socket communication with Python
Data analysis with python 2
Try scraping with Python.
Learning Python with ChemTHEATER 03