Raspberry Pi: MongoDB + Python

pip install pymongo

Create a file with a test code

nano test.py

Add the code

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")

mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mycol.drop()

mydict = { "name": "John", "address": "Highway 37" }
x = mycol.insert_one(mydict)
x = mycol.find_one()

print(x)

Save and run

python test.py

The result should be something like this

{'_id': ObjectId('............'), 'name': 'John', 'address': 'Highway 37'}

Source:
https://www.mongodb.com/docs/php-library/current/tutorial/connecting/
https://www.w3schools.com/python/python_mongodb_getstarted.asp

Leave a Reply

Your email address will not be published.

Yandex.Metrica