Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB-pymongo.errors.CursorN

Shulou Source: shulou.com Published: 2022-06-01 16:16:19 10月01日 Update

Python, python3.

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

Demos = db ['demo'] .find ({}, {"_ id": 0}) for cursor in demos: do_something ()

However, when the do_something function takes too long and does not operate on the cursor for a long time, cursor times out on the mongodb server.

Solution

1. Set no_cursor_timeout = True. Never time out. The cursor connection will not be closed actively and needs to be closed manually.

Demos = db ['demo'] .find ({}, {"_ id": 0}, no_cursor_timeout = True) for cursor in demos: do_something () demo.close () # close the cursor

2. Set the number of documents returned by batch_size. By default, it should be 20 documents (I can't remember 233333). You can set it smaller.

# return only 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 occur for more than 10 minutes and still does not return, such as you do some very time-consuming operations in do_something, depending 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 finds out how many city fields there are?

Tags: Operator document data method cursor initiative function atom field actual that is situation manual database scheme condition never knowledge knowledge point result Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Microsoft Linux MySQL macOS