Python-to-mongodb interaction---Lookup

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.