i am trying to get a string from all documents in a mongo db collection, but when it starts it give me an error:
106 | * not subject to semantic versioning compatibility guarantees and may change at any time.
107 | *
108 | * @public
109 | **/
110 | constructor(message, options) {
111 | super(message, options);
^
error: Cannot use a session that has ended
at new MongoError (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodbliberror.js:111:9)
at new MongoDriverError (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodbliberror.js:209:9)
at new MongoAPIError (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodbliberror.js:238:9)
at new MongoExpiredSessionError (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodbliberror.js:403:9)
at applySession (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibsessions.js:708:16)
at prepareCommand (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibcmapconnection.js:169:49)
at C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibcmapconnection.js:259:30
at C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibcmapconnection.js:313:26
at command (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibcmapconnection.js:311:23)
at C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibsdamserver.js:167:40
my function:
export async function returnAllRep(): Promise<string[]> {
const { client, database } = await connectToDatabase();
const collection = database.collection('chains');
try {
const docs = await collection.find().toArray();
const ids = docs.map(doc => doc.id);
return ids;
} catch (error) {
console.error('Errore durante la recuperazione dei documenti:', error);
throw error;
} finally {
await client.close();
}
}
I have trying to add debug when it connect to the db, it says it’s connected but then it crash
If i try with a single document it works, but i need all the document
2
Answers
To connect your client to the MongoDB cluster, and retrieve your database, I would use the following syntax instead.
It seems like the way you are getting the database is different.
The error you are encountering, "Cannot use a session that has ended,", typically happens when you are trying to use a MongoDB session that has already been closed or expired. This can occur due to the session timing out or if you manually ended the session and then attempted to use it again.
To resolve this, you need to ensure that you do not reuse a session after it has been ended. Here’s a basic example of how to correctly use a session with MongoDB, making sure it is properly created, used, and ended after the operations.
Below is an example of how to get strings from all documents in a MongoDB collection:
Connection Setup:
A function to retrieve documents from the collection, keeping session handling in mind.
Key Points:
startSession
: We explicitly start a new session.commitTransaction
: Commits the run if everything goes well.endSession
: Properly ends the session by either committing or aborting the transaction based on the operation’s success.Make sure you replace
'your-mongodb-uri'
,'your-database'
,'your-collection'
, and'yourField'
with actual values from your MongoDB setup.Additional Tips:
If you handle sessions carefully and ensure they’re properly managed and closed, you should be able to avoid the
MongoExpiredSessionError
.