Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9317849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:58:44+00:00 2026-06-19T02:58:44+00:00

I would like to know the most efficient way to create /close MongoDb Connections.

  • 0

I would like to know the most efficient way to create /close MongoDb Connections.

I have a set of Restful services (created using c#), some of them get data form a MongoDb and some POST data in.

   public static MongoDatabase GetDatabase()
   {
      MongoServerSettings settings = new MongoServerSettings();
      settings.Server = new MongoServerAddress("localhost", 27017);
      MongoServer server = new MongoServer(settings);
      var database = server.GetDatabase("RapidDataStream");
      return database;
   }

I use the above connection form each of my services, for example a service to list all sensors.

public List<Sensor> getPublicSensors()
{
            List<Sensor> sensors = new List<Sensor>();
            MongoDatabase mySensor = GetDatabase();
            var query = Query.And(Query.EQ("User", BsonValue.Create("Public")));
            var mySensorRecords = mySensor.GetCollection("sensor_meta_data").Find(query);
            DataSet ds = new DataSet();

            foreach (var rec in mySensorRecords)
            {
                Sensor sensor = new Sensor();
                sensor.sensorId = rec["EnvId"].ToString();

                for (int i = 2; i < rec.ElementCount; i++)
                {
                    SensorObject so = new SensorObject();
                    so.fieldName = rec.GetElement(i).Name.ToString();
                    so.value = rec.GetElement(i).Value.ToString();
                    sensor.sensorObject.Add(so);
                }
                sensors.Add(sensor);
            }
            return sensors;
}

My question is that whether this is an efficient design or else how can I improve it?

Many Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:58:45+00:00Added an answer on June 19, 2026 at 2:58 am

    I believe Mongo (or the driver) handles some things for us, so I use the Create method:

    MongoServer.Create(....)
    

    instead of creating a new instance. The Create method will create a new instance or return an existing instance (it is unique per server settings).

    In fact, just to show you the actual method I use currently:

    internal static MongoDatabase DB(IMongoConfig config)
    {
        return MongoServer
               .Create(config.ConnectionString)    //This bit is getting the MongoServer
               .GetDatabase(config.DatabaseName);  //This bit gets the Database, which is returned
    }
    

    IMongoConfig is just a little DTO I use to specify, as you can see, the connection string and DB name I need – this combined with the workings of MongoServer.Create make it quite easy to handle connections to multiple databases from the same running code base.

    public interface IMongoConfig
        {
            string ConnectionString { get; set; }
            string DatabaseName { get; set; }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know what is the most efficient way to create a
I would like to know the most efficient way of emptying an associative array
I would like to know the most efficient way of storing days of the
I would like to know what is the most efficient and practical way of
I would like to know what is the most efficient way to transfer BitmapData
I would like to know what the most efficient c++ implementation of the following
What would be the most efficient way to implement a matlab like function to
I know that this kind of issue is most discussed. But I would like
Would like to know how to hide an div after a set of css3
I would like to know how. I have looked at this topic , and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.