When Python interacts with the MONGO database, there are some minor issues to note when looking:
Code:
1 fromPymongoImport*2 defFind_func ():3 #Create a Connection object4Client = Mongoclient (port=27017,host='localhost')5 #Select the database to use6db =client.test7 #perform a query operation8res = Db.students.find_one ({'name':'John Doe'})9 #The returned res is a collectionTen One Print(RES) A #The results are as follows:
Use Find_one to return a document (collection)
When you use Find, an object is returned:
Change the code for line eighth:
# The eighth line is replaced by:res = db.students.find ({'name':' John Doe '}) Print(res)# See what executable methods of res print(dir (res))
You can see that res is an object
(⊙o⊙) ..., dir (res) A lot more, we should be concerned that there is a __iter__ method, which shows that res is an iterative object. means that you can use the for in to traverse.
for inch Res: Print (i)
Python-to-mongodb interaction---Lookup