Connecting to mongodb sing C# quick tour not creating db or collection

Connecting to mongodb sing C# quick tour not creating db or collection

I’m going through the mongoDB Driver Documentation Quick Tour for the first time. Specifically the 2.4 version.
I’ve created a fresh mongodb instance at the 192.168.1.50 address, and it appears to be running correctly.
The MongoDB documentation gives the following example:
var client = new MongoClient(“mongodb://192.168.1.50:27017”);
#It’s ok if the database doesn’t yet exist. It will be created upon first use
var database = client.GetDatabase(“testDB”);
#It’s ok if the collection doesn’t yet exist. It will be created upon first use.
var collection = database.GetCollection(“testCollection”);

However, when I go on my server, and I enter the mongo console
mongo

And I list the databases using
show dbs

The output is only
admin 0.000GB
local 0.000GB

Is there anything else I should have done to make this work? I’m getting no errors on try/catch, and it appears to be running fine.

Troubleshooting
So far I’ve confirmed that mongodb is running by using the following:
netstat -plntu

Related:  How do i import json file that was exported from parse.com into mongodb?

Shows mongod running on 27017 in the LISTEN state.

I’d also be interested in knowing if there’s a way on the mongodb server to view live connections, so I could see if it were actually successfully connecting.

Solutions/Answers:

Solution 1:

Well the problem is that you need to create almost one collection in order to persist the created database (weird right?) i tested it with robomongo and works in that way.

The problem is that GetCollection method is not creating the target collection, you can try with this code:

    static void Main(string[] args)
    {
        var client = new MongoClient("mongodb://192.168.1.50:27017");
        //# It's ok if the database doesn't yet exist.  It will be created upon first use
        var database = client.GetDatabase("test");
        //# It’s ok if the collection doesn’t yet exist. It will be created upon first use.

        string targetCollection = "testCollection";
        bool alreadyExists = database.ListCollections().ToList().Any(x => x.GetElement("name").Value.ToString() == targetCollection);

        if (!alreadyExists)
        {
            database.CreateCollection(targetCollection);
        }

        var collection = database.GetCollection<BsonDocument>(targetCollection);

    }

Solution 2:

It turns out that a method I had found on how to set multiple bindIp’s was incorrect. The problem wasn’t with the C# at all. I found the solution here

Related:  MongoDB / Meteor / Export MONGO_URL to deployed applications

In case that ever goes away, here’s the current settings I had to follow for multiple ip’s

edit file /etc/mongod.conf

Wrap the comma-separated-Ips with brackets

bindIp = [127.0.0.1, 192.168.184.155, 96.88.169.145]

My original code worked fine, I just didn’t have the brackets on the bindIp.

References