Skip to content

aggregate_with_session fails when collection size > batchSize #482

Closed
prisma/prisma-engines
#2334
@Weakky

Description

@Weakky

Overview

Instantiate a MongoDB DB with a collection of N documents. Run a simple aggregation against that collection with a batchSize that is lower than N and that's not a multiple of N.

Examples:

  • N = 100, batchSize = 40
  • N = 102, batchSize = 101 (default value)

For all those cases, cursor.next(&mut session) will return the following error:

Error: Command failed (CursorNotFound): cursor id XXXXXXXXX not found)

Reproduction

Below is a minimal reproduction example:

use anyhow::Result;
use mongodb::{
    bson::{doc, Document},
    options::{AggregateOptions, ClientOptions},
    Client, Collection,
};
use tokio;

#[tokio::main]
async fn main() -> Result<()> {
    let client_options = ClientOptions::parse("MONGO_URL").await?;
    let client = Client::with_options(client_options)?;
    let mut session = client.start_session(None).await?;
    let db = client.database("DATABASE_NAME");
    let coll: Collection<Document> = db.collection("COLLECTION_NAME");
    // Default batchSize is 101. Have at least 102 documents for the error to happen
    // or set a `batchSize` according to the rules above depending on the number of documents in your collection
    let opts = AggregateOptions::builder().build();
    let mut res = coll
        .aggregate_with_session(
            vec![doc! {
                "$project": { "_id": 1 }
            }],
            opts,
            &mut session,
        )
        .await?;
    let mut docs = vec![];

    while let Some(result) = res.next(&mut session).await {
        match result {
            Ok(document) => docs.push(document),
            Err(e) => return Err(e.into()),
        }
    }

    println!("Number of documents: {}", docs.len());

    Ok(())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    tracked-in-jiraTicket filed in Mongo's Jira system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions