Mongo Go Driver advice

Hi,

I am working on an application that tracks medication, dosage, prescription, renewal dates etc, I am using MongoDB and trying to use the MongoDB Go Driver.

The issue I am having is that every example I can find online to use this driver does everything in the main func, whereas my app will need to manage the data in the handler funcs for a Web server. I am struggling to see how to keep the connection open when it is opened in the main func so I can just add in the data, search it, update or delete later on. I would rather not create a global variable, though i will if there is no other real choice. I have currently got it working by opening and closing the connection every time I need to reference the database. For small amounts, this isn’t too much of an issue, overhead-wise, however as data entry requirements increase, this will become a problem. Does anyone know the best way to manage this? or can point me to a resource that could show me how to do this?

Appreciate any advice on this, as I am fairly new to working with Mongo.

Dan

1 Like

have a connectdb function which will just return master session attach it to Session global variable .and call this only once in main

Then from dal or halders you just use Session.Copy().DB(dbName) and close every whrere

2 Likes

Hi,

Thanks for that, not sure why that didn’t occur to me at all. Much appreciated.

Dan

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.