Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental: expose the cursor in find() queries #57

Closed
trenton42 opened this issue May 22, 2014 · 4 comments
Closed

Experimental: expose the cursor in find() queries #57

trenton42 opened this issue May 22, 2014 · 4 comments

Comments

@trenton42
Copy link
Contributor

I have been thinking for quite a while about being able to run queries without loading the entire result set into memory. Here's what I came up with (view the Changes):

collection.find_with_cursor returns a deferred which results in a tuple: (docs, deferred). If there are no more documents remaining in the current query, a deferred will be returned which results in this tuple: ([], None)

For example:

docs, d = yield collection.find_with_cursor()
# collection.find(cursor=True) also works

while docs:
    # Do something with this page of docs here
    for i in docs:
        pass
    # Get the next set of docs from the query.
    docs, d = yield d

I have already implemented this (here), but let me know what you think, or if there are any improvements that could be made on this, then I will send over a pull request

@fiorix
Copy link
Collaborator

fiorix commented May 22, 2014

Seems interesting, but how does the cleanup work in case something fails (e.g. exception) and you have the cursor no longer accessible?

@trenton42
Copy link
Contributor Author

That's a good question. It is basically the same as running one query per loop. If an exception occurs in the middle, it would be like failing in the middle of a generator. Mongo itself will expire a cursor after an amount of time (cursor expiration)

@psi29a
Copy link
Contributor

psi29a commented Dec 30, 2014

@trenton42: Why not send a pull request? I'de like to see this.

@trenton42
Copy link
Contributor Author

@psi29a Pull request submitted!

@psi29a psi29a closed this as completed Mar 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants