Tag Archives: getting started

Creating a C# Client, and Listing, and Adding Data in MongoDB

In this previous post I wrote about how you can install MongoDB, and use the built in clients to interrogate the data. In this post I’ll cover setting up a simple C# client to interrogate the database.

Bring in a NuGet Package

As with many integrations in the last 10 years, 90% of the job is installing the right NuGet package:

Install-Package MongoDB.Driver

Once that’s installed, you should have access to the SDK. You’ll need the following using statement:

using MongoDB.Driver;

Connecting to the MonoDB Instance

It actually took me a while to work out the correct format here; for the default instance, you can simply use this for the connection string:

mongodb://localhost:27017

So to connect to the DB, you would use this:

MongoClient dbClient = new MongoClient("mongodb://localhost:27017");
var db = dbClient.GetDatabase("testdb");

See the referenced previous post if you’re interested where testdb came from.

Collections

As we saw in the previous post, Mongo works around the concept of collections – it’s roughly analogous to a table. The list the collections:

MongoClient dbClient = new MongoClient("mongodb://localhost:27017");
var db = dbClient.GetDatabase("testdb");

var collections = await db.ListCollectionsAsync();
            
foreach (var col in collections.ToList())
{
    Console.WriteLine(col.ToString());
}

Let’s see what inserting data would look like.

Inserting Data

In fact, inserting data is very straightforward. We need to introduce a new type here, called a BsonDocument. BSON is a Binary JSON document, and to all intents and purposes, it’s JSON; this means that you can create a document such as this:

            var document = new BsonDocument()
            {
                { "test", "test1" },
                { "test2", "test2" },
                { "test3", "test3" }
            };

Or, indeed, any valid JSON document.

To insert this into the DB, you would just call InsertOne:

            var collection = db.GetCollection<BsonDocument>("testcollection");
            await collection.InsertOneAsync(document);

You can insert many records by calling InsertMany:

            var documents = new List<BsonDocument>()
            {
                new BsonDocument()
                {
                    { "test", "test1" },
                    { "test2", "test2" },
                    { "test3", "test3" }
                },
                new BsonDocument()
                {
                    { "Date", DateTime.Now },
                    { "test2", 12 },
                    { "test3", "hello" }
                }
            };

            var collection = db.GetCollection<BsonDocument>("testcollection");
            await collection.InsertManyAsync(documents);

References

https://www.mongodb.com/blog/post/quick-start-c-sharp-and-mongodb-starting-and-setup

Getting Started with Gatsby

Although I’ve used React before, Gatsby was quite new to me. This post was born from the creation of this website. We needed something that would let us create a nice looking site very quickly. Gatsby looked like it might be the answer.

Installation

To install gatsby, you can do so from npm:

npm Install -g gatsby-cli

Creating a Project

You can start from scratch, but they have a number of template projects here for you to clone.

Running

Once you’ve cloned the template, you can run it:

gatsby develop

You’ll need to run this from whichever directory you have package.json in (typically the root directory).

That will run your project locally (usually on 8080).

Deploying

Once you’re happy that this works, you can deploy the site using the build command:

gatsby build

This will create a /public folder, which is, effectively, your website – you can now just copy this to wherever you’re serving the site from and you’re good to go.