I am starting a new series of my blogs again, after a long time. This part describes how to get started with MongoDB in C# for beginners. This will include basic introduction to NoSQL, MongoDB, a C# console application and Azure CosmosDB. I don't want to bore you with information, so let's get started.
First of all, what is NoSQL? NoSQL (originally referring to "non SQL", "non relational" or "not only SQL") is a query language which uses a different mechanism of storage and retrieval of data in non-relational databases. Unlike the tabular relations used in relational databases, NoSQL stores data in a key-value pair. A simplest example of NoSQL would be like :
{ "FirstName" : "Alex", "Age" : 25, "Email" : "[email protected]" }
Wait a second! This example rings a bell, doesn't it? It looks just like JSON data. You are absolutely right. Most of the non-relational databases use JSON as their preferred input/output format. If you don't have the knowledge of JSON, no worries. Just study a little bit about
JSON and you will be ready to go.
Next is the platform where these NoSQL statements will be executed. Like SQL statements are executed in the relational databases (like
Microsoft SQL Server, Oracle, MySQL, etc.), the NoSQL statements are executed in non-relational databases, such as - MongoDB, RavenDB, CouchDB, DocumentDB, etc. In our write-up, we will be focusing on MongoDB. Before getting started with MongoDB, you might want to know when and where to use NoSQL. Although, this question is quite debatable. Still, you can refer to
Ted Neward's Blog which will clear out some mist.
MongoDB (from humongous) is a free and open source database, published under the GNU Affero General Public License which stores data in a document-oriented format. MongoDB uses JSON-like documents with schemas. It stores the data in the BSON (Binary JSON) form in the database, but uses JSON as preferred input/output format.
As mentioned earlier, MongoDB is free and open source software which can be easily downloaded from the
official website. Just
download the Community Server edition MSI installer for Windows and install.
Once installed, you need to verify whether everything works fine or not. By default, MongoDB wants to store data in the default file system path, C:\data\db, but this is configurable with a text file passed by name on the command line via --config.
Again by default, C:\data\db directory won't exist and installing MongoDB won't create this directory. You have to manually create this directory or configure the system path to your preferred directory. If you don't do it, the MongoDB Server won't work properly. Let's not get into the hassle with configurable system path and keep it simple with the default system path.
Now, open up the command prompt by pressing Windows + R keys and type cmd and press Enter. Navigate to the directory "C:\Program Files\MongoDB\Server\3.4\bin" by executing these commands,
cd ..\..
cd "C:\Program Files\MongoDB\Server\3.4\bin"
If you navigate to this directory in terms of folder view, you will notice a bunch of files out of which you need only two executable files to get you started - mongod.exe and mongo.exe.
The mongod.exe is the database process itself and the mongo.exe is the command-line client for executing NoSQL statements. Now in the command prompt, type "mongod.exe" and press Enter.
If the text "waiting for connections on port 27017" comes up, you will know the MongoDB server is up and running. Minimize this command prompt window and open up another command prompt window. Navigate to the same directory "C:\ProgramFiles\MongoDB\Server\3.4\bin" and fire up "mongo.exe".
This is the client for executing NoSQL commands. So, let's start writing few commands for better understanding.
db.adminCommand( { listDatabases: 1 } )
This command displays the list of databases in MongoDB Server. The default database is "test".
db.getCollectionNames()
This command displays the list of collection in the current database. The default collection is "things".
Here, you can think of collection as table. These are more database related commands. For further details, you can refer to this
link.
p1 = {"PersonFirstName":"Alex","PersonAge":28}
db.things.save(p1)
The first command creates a temporary document p1 which contains the actual data to be stored in the collection "things". Here, document is not a file, like word(.docx) file, text(.txt) file, etc. Rather it refers to the row/tuple that contains the data. The keys ("PersonFirstName" or "PersonAge") in it are known as elements of the document. The second command actually stores p1 temporary document in the collection "things" permanently.
db.things.find()
This command displays the list of documents in the collection "things" (a maximum count of 20 documents).
Here, you notice that there is another element "_id" that you didn't create. It is auto-generated for uniquely identifying the document. You can think of it as "Primary Key".
findP = {"PersonFirstName":"Alex"}
db.things.find(findP)
This command displays the specific document with the passed parameters.
testP = {"PersonAge":28}
setP = {"PersonFirstName":"Brandt"}
db.things.updateOne(testp,{ $set : setp })
This command updates the element "PersonFirstName" from "Alex" to "Brandt" of the specific document with the "PersonAge" as 28.
db.things.deleteOne(testP)
This command deletes the specific document.
These are the basic CRUD operation commands. For more details, you can refer to this
link. Now, to close the mongo.exe client, type "exit" and press Enter. Close this window. Again, to close mongod.exe process, press Ctrl + C.
That's all. I hope this article helps you to get started with MongoDB.