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

MongoExpiredSessionError: Cannot use a session that has ended when using Model.collection.find_.populate #15195

Open
2 tasks done
KhaledMosaad opened this issue Jan 22, 2025 · 0 comments

Comments

@KhaledMosaad
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.9.x

Node.js version

22.x

MongoDB server version

7

Typescript version (if applicable)

No response

Description

When using Model.collection.findOne.populate(....) the Driver shutdown the session that running and end up with MongoExpiredSessionError: Cannot use a session that has ended
here's the full exception:

MongoExpiredSessionError: Cannot use a session that has ended

      at applySession (node_modules/mongodb/src/sessions.ts:1101:12)
      at Connection.prepareCommand (node_modules/mongodb/src/cmap/connection.ts:380:40)
      at Connection.sendCommand (node_modules/mongodb/src/cmap/connection.ts:498:26)
          at sendCommand.next (<anonymous>)
      at Connection.command (node_modules/mongodb/src/cmap/connection.ts:617:22)
      at Server.command (node_modules/mongodb/src/sdam/server.ts:337:32)
      at FindOperation.execute (node_modules/mongodb/src/operations/find.ts:130:12)
      at tryOperation (node_modules/mongodb/src/operations/execute_operation.ts:278:14)
      at executeOperation (node_modules/mongodb/src/operations/execute_operation.ts:112:12)
      at FindCursor._initialize (node_modules/mongodb/src/cursor/find_cursor.ts:90:22)
      at FindCursor.cursorInit (node_modules/mongodb/src/cursor/abstract_cursor.ts:896:21)
      at FindCursor.fetchBatch (node_modules/mongodb/src/cursor/abstract_cursor.ts:934:7)
      at FindCursor.next (node_modules/mongodb/src/cursor/abstract_cursor.ts:569:9)
      at Collection.findOne (node_modules/mongodb/src/collection.ts:524:17)

Don't know if it's possible to use .populate with .collection methods or not, but the error is not an obvious one and should be more clear

Steps to Reproduce

Say we have a collection named Schools and another one named Classes

  • Create school document with classIds inside it
await Schools.create({ name: 'example_name', classIds: [ObjectId('1'),ObjectId('3'),ObjectId('2')]});
  • Query the school collection and populate the classIds
const schools = Schools.collection.findOne({_id:someSchoolId}).populate('classIds');

the exception will occur

Expected Behavior

No response

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

1 participant