Skip to content

DOCSP-48163: Add async examples for Read pages #227

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

Closed
wants to merge 5 commits into from

Conversation

mcmorisi
Copy link
Collaborator

@mcmorisi mcmorisi commented Mar 17, 2025

Pull Request Info

PR Reviewing Guidelines

JIRA - https://jira.mongodb.org/browse/DOCSP-48163

Staging Links

  • read
  • read/change-streams
  • read/count
  • read/cursors
  • read/distinct
  • read/project
  • read/retrieve
  • read/specify-a-query
  • read/specify-documents-to-return
  • Self-Review Checklist

    • Is this free of any warnings or errors in the RST?
    • Did you run a spell-check?
    • Did you run a grammar-check?
    • Are all the links working?
    • Are the facets and meta keywords accurate?

    Copy link

    netlify bot commented Mar 17, 2025

    Deploy Preview for docs-pymongo ready!

    Name Link
    🔨 Latest commit 851ab6c
    🔍 Latest deploy log https://app.netlify.com/sites/docs-pymongo/deploys/67e16fdb22dcde00082af48b
    😎 Deploy Preview https://deploy-preview-227--docs-pymongo.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify site configuration.

    Copy link
    Collaborator

    @norareidy norareidy left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    LGTM with small things

    # start-tailable-cursor-async
    oplog = client.local.oplog.rs
    first = oplog.find().sort('$natural', pymongo.ASCENDING).limit(-1).next()

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggested change
    first = oplog.find().sort('$natural', pymongo.ASCENDING).limit(-1).next()
    first = await oplog.find().sort('$natural', pymongo.ASCENDING).limit(-1).next()

    cursor = oplog.find({'ts': {'$gt': ts}},
    cursor_type=pymongo.CursorType.TAILABLE_AWAIT)
    await while async cursor.alive:

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggested change
    await while async cursor.alive:
    while cursor.alive:

    # You end up here if the find() method returns no documents, or if
    # no new documents are added to the collection for more than 1 second.
    time.sleep(1)

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggested change
    time.sleep(1)
    await asyncio.sleep(1)

    # start-distinct
    results = await restaurants.distinct("borough")

    async for restaurant in results:

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggested change
    async for restaurant in results:
    for restaurant in results:

    distinct() returns a list.

    "cuisine": "Italian"
    })

    async for restaurant in results:

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggested change
    async for restaurant in results:
    for restaurant in results:

    print(count)
    # end-estimated-count
    # start-distinct
    results = collection.distinct("<field name>")

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggested change
    results = collection.distinct("<field name>")
    results = await collection.distinct("<field name>")

    # start-distinct
    results = collection.distinct("<field name>")

    async for document in results:

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggested change
    async for document in results:
    for document in results:


    # end example code here

    client.close()

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggested change
    client.close()
    await client.close()

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Realize I forgot to wrap this in an async function (Python complains otherwise) so I can also do that

    @mcmorisi mcmorisi requested a review from NoahStapp March 24, 2025 14:44
    @mcmorisi mcmorisi closed this Mar 24, 2025
    @mcmorisi mcmorisi deleted the DOCSP-48163-async-read branch March 24, 2025 15:55
    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

    Successfully merging this pull request may close these issues.

    3 participants