#Try this:
from pymongo import MongoClient
# connect to the MongoDB
connection = MongoClient('mongodb://127.0.0.1:<port>')
# connect to test collection
db = connection.test
# create dictionary
student_record = {}
# save rec to dict
student_record = {'name': 'John Doe','grade': 'A+'}
#insert the record
db.test.insert(student_record)
# find all documents
results = db.test.find()
# display documents from collection
for record in results:
out_name = str(record['name'])
out_grade = str(record['grade'])
print(out_name + ',' + out_grade)
# close the connection to MongoDB
connection.close()
相关问题 更多 >
编程相关推荐