Skip to content

Instantly share code, notes, and snippets.

@dubeyji10
Last active July 20, 2022 08:03
Show Gist options
  • Save dubeyji10/4e33ab6baeb7a4a2721519102b76f65a to your computer and use it in GitHub Desktop.
Save dubeyji10/4e33ab6baeb7a4a2721519102b76f65a to your computer and use it in GitHub Desktop.
Insertion of one document using python mongo client connection
from pprint import pprint
import json
from pymongo import MongoClient
#mongodb://127.0.0.1:27017/
client = MongoClient(host="localhost", port=27017)
print("1.connected to mongodb")
db = client.landTest
print("2.db : ",db)
# print("")
collection = db.landTest
print("\n\n3.collection:",collection)
print('-'*25)
testInsert = {
"address": "Avenue A.-F.-Dubois 2",
"commune": "Meyrin",
"category": "Habitation un logement",
"type": "Privé",
"age": None,
"area1": 1669.0,
"area2": 65.0,
"totalarea": 65.0,
"owner": "DAVEL Gisela Inge",
"bno": "33/11045",
"name": "TIMM",
"ho": "",
"date": "1942-05-16"
}
print('-'*25)
print("4. inserting one document: \n")
# jsonInsert = json.loads(testInsert)
# pprint(jsonInsert)
pprint(testInsert)
print('\n\n')
result = collection.insert_one(testInsert)
print("5.result : ",result)
client.close()
print("\n\n--connection closed--")
@dubeyji10
Copy link
Author

Result in mongo console

success-insert.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment