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

Fix aggregate cursor als #15094

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

IchirokuXVI
Copy link

Summary

Aggregate cursors didn't automatically add session if it was present in AsyncLocalStorage. Also the options weren't the same for exec and cursor so I added a function to make them the same just like in QueryCursor.

Examples

const mongoose = require("mongoose");
mongoose.set("transactionAsyncLocalStorage", true);

mongoose.connect("mongodb://127.0.0.1:27017/cursortest");

const test = async () => {
  await mongoose.connection.transaction(async (session) => {
    const TestModel = mongoose.model(
      "Test",
      new mongoose.Schema({ name: String })
    );

    mongoose.set("debug", true);
    await TestModel.aggregate([{ $match: {} }]);
    const cursor1 = TestModel.aggregate([{ $match: {} }]).cursor();
    const cursor2 = TestModel.aggregate([{ $match: {} }])
      .session(session)
      .cursor();

    await cursor1.next();
    await cursor2.next();
    mongoose.set("debug", false);

    /*
        From the debug logs:
            Mongoose: TestModel.aggregate([ { '$match': {} } ], { session: ClientSession("bb843cfeb01d4ec3ad0373d2b7d3db20") })
            Mongoose: TestModel.aggregate([ { '$match': {} } ], { cursor: {}, session: ClientSession("bb843cfeb01d4ec3ad0373d2b7d3db20")})
            Mongoose: TestModel.aggregate([ { '$match': {} } ], { session: ClientSession("bb843cfeb01d4ec3ad0373d2b7d3db20"), cursor: {}})

        Before the fix the first cursor didn't have a session but aggregate and second cursor had a session.
    */
  });
};

test()
  .then(() => console.log("Finished"))
  .catch((error) => console.error(error))
  .finally(() => process.exit(0));

@IchirokuXVI IchirokuXVI force-pushed the fix-aggregate-cursor-als branch from 46fc7ed to 51e6b08 Compare December 13, 2024 17:09
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.

1 participant