Connect to MongoDB in C# with Credentials

0 votes

Hi I'm trying to connect to my remote MongoDB on AppWarpS2 with Visual Studio 2013 in c# using the latest Official MongoDB supported driver for MongoDB. The problem I'm having is I'm unable to insert a collection or possibilly even establishing the connection.

I'm using the following code to create mongo credential (DBName, User, Password)

 mongoCredential = MongoCredential.CreateMongoCRCredential("MongoDB", 
                            "xxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxx");

Setup credentials List

 mongoClientSettings = new MongoClientSettings 
        { 
            Credentials = new[] { mongoCredential }, 
            Server = new MongoServerAddress("xx.xxx.xx.xxx", 38180)
        };

Add our mongo server credentials to our client

 mongoClient = new MongoClient(mongoClientSettings);

Then to connect:

 private void button1_Click(object sender, EventArgs e)
    {
        // Connect to database
        mongoDatabase = mongoClient.GetDatabase("MongoDB");
    }

Now I try and insert a collection:

 private void button4_Click(object sender, EventArgs e)
    {
        // Create a collection
        mongoDatabase.CreateCollectionAsync("aCollection");
    }

This method is not working for we as when I run mongo from prompt and connect to remote db with credentials then run db.stats() it returns an empty db:

 > db.stats()
 {
    "db" : "MongoDB",
    "collections" : 0,
    "objects" : 0,
    "avgObjSize" : 0,
    "dataSize" : 0,
    "storageSize" : 0,
    "numExtents" : 0,
    "indexes" : 0,
    "indexSize" : 0,
    "fileSize" : 0,
    "ok" : 1
 }
 >

MongoDB is not my thing and any help here would be much appreciated as I'm going round in circles with this?

 

asked Jun 28, 2015 in App42 Cloud API-BaaS by wiredvoltage (84 points)

1 Answer

0 votes

HI

It looks like you are just initaiting the connection to mongo and actually not doing any insert or update. Please see here to use it properly in your code http://mongodb.github.io/mongo-java-driver/2.13/getting-started/quick-tour/

Alos, if you are looking something as a cloud service storage for your game data, you can use our Storage Service for the same.

Let me know if it helps.

 

Thanks

AJay

answered Jul 1, 2015 by ajay123 (899 points)
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...