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