I have been working on creating new documents from existing ones within a collection in my database. Specifically, I am flattening a field from the original document during this process. Initially, everything ran smoothly using mongosh but now that I need to execute the script on a gcp function, I'm encountering some challenges. The error arises when attempting to close the connection:
MongoExpiredSessionError: Cannot use a session that has ended
As a result, the document is not added to the new collection. Interestingly, removing DB.close()
allows the documents to be added successfully, however, the program does not complete.
I made sure to place await statements where necessary for interactions with the DB, but unfortunately, it did not resolve the issue. Below is the code structure: The main function calls connectMongo, which establishes a connection to the MongoDB and initiates the execute function. The execute function retrieves documents from the original collection and passes them individually to the flatten function. The flatten function is responsible for the flattening operation and adding the new document to the new collection.
Here's an overview of the code setup:
Print statements within the code work regardless of whether DB.close()
is included or not.
const {MongoClient} = require('mongodb');
const uri = "mongodb://<myURI>"
const DB = new MongoClient(uri);
var collections;
//---------------------------------------------------------------------------------------------
execute = async function(db){
docs = await db.collection('observations6').find()
await docs.forEach(flatten)
}
//---------------------------------------------------------------------------------------------
flatten = async function(myDoc){
forms = myDoc.properties.forms
for(var i = 0; i < forms.length; i++){
FormObj = {
parent_id : myDoc._id+'_form_'+i,
eventId: myDoc.eventId,
userId: myDoc.userId,
deviceId: myDoc.deviceId,
createdAt: myDoc.createdAt,
lastModified : myDoc.lastModified,
type: myDoc.type,
geometry: myDoc.geometry,
favoriteUserIds: myDoc.favoriteUserIds,
states: myDoc.states,
attachments: myDoc.attachments,
formId: forms[i]['formId'],
}
console.log('Created form object')
field_count = 0
retry = 0
while (retry <= 10){
if (!forms[i].hasOwnProperty('field'+field_count)){
field_count += 1
}
retry += 1
}
console.log(`Did ${field_count} retry(s)`)
while(forms[i].hasOwnProperty('field'+field_count)){
FormObj['field'+field_count] = forms[i]['field'+field_count]
field_count+=1
}
console.log('Added field properties')
parent_id = myDoc._id + '_form_' + i
await collections.collection('observations6_flatten').updateOne({parent_id},{$set: {FormObj}}, {upsert:true})
console.log('Added object to collection')
}
}
//---------------------------------------------------------------------------------------------
async function connectMongo() {
try {
// Connect to the MongoDB cluster
await DB.connect();
// Make the appropriate DB calls
collections = await DB.db('magedb')
//console.log(collections)
await execute(collections)
} catch (e) {
console.error(e);
}
}
//---------------------------------------------------------------------------------------------
//Main call
main = async function(){
await connectMongo()
.then(()=>{
DB.close()
})
}
main()
The script returns the following errors:
aortiz@WW-593 MINGW64 ~/Documents/GitHub/engineering_etl (main)
$ node flattening/flattening.js
Created form object
Did 11 retry(s)
Added field properties
Created form object
Did 0 retry(s)
Added field properties
...
C:\Users\aortiz\Documents\GitHub\engineering_etl\node_modules\mongodb\lib\sessions.js:655
...
If I exclude DB.close()
, the output adds objects to the collection but fails to exit.
This discrepancy indicates that the error occurs during the line:
await collections.collection('observations6_flatten').updateOne({parent_id},{$set: {FormObj}}, {upsert:true})
It raises questions about why the connection terminates before reaching this point.