Mongodb-pymongo.errors.cursornotfound:cursor

Source: Internet
Author: User
Tags mongodb server


python, python3.
First get all the data from the database db ['test']. Find ({}, {_id: 0}), and then for loop the results

demos = db ['demo']. find ({}, {"_ id": 0})

for cursor in demos:

         do_something ()
However, when the do_something function takes too long, the cursor has not been operated for a long time, causing the cursor to time out on the mongodb server

solution

1. Set no_cursor_timeout = True, never time out, the cursor connection will not be closed actively, you need to close it manually

demos = db ['demo']. find ({}, {"_ id": 0}, no_cursor_timeout = True)

for cursor in demos:

        do_something ()

demo.close () # close the cursor
2. Set batch_size to return the number of documents, the default should be 20 documents (233333 can not remember), you can set a smaller

#Only return one document at a time

demos = db ['demo']. find ({}, {"_ id": 0}). batch_size (1)

for cursor in demos:

        do_something ()
Note: This method may still appear for more than 10 minutes without returning. For example, if you perform some very time-consuming operations in do_something, the specific method used depends on the actual situation.

Additional knowledge points:
mongodb conditional operators, "$ lt", "$ lte", "$ gt", "$ gte", "$ ne" are all comparison operators,
Corresponds to "<", "<=", ">", "> =", "! =".
Atomic operators: "$ and", "$ or", "$ nor".

【2】:
db.runCommand (
{
distinct: "sofang_xinfang", key: "city"
}
) --- distinct find out how many kinds of city field there are?

Original address: http://blog.51cto.com/13000661/2117066

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.