In this series of posts, I’ll describe how to build a .Net application with MongoDB as a back-end database.
On this first post, I will briefly introduce about MongoDB and environment setup to work with MongoDB.
MongoDB is an open-source document database, and leading NoSQL database. It is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document. If you want to learn more about MongoDB you can visit tutorials point website to get started.
Now, let’s start to set up environment. First download and install MongoDB setup file from official site. Installation process is same as you do like other normal application. Now let’s keep the path of mongodb to environmental variables so we can access from our cmd prompt or power shell using mongo command. Here are steps:
- Get the path of bin folder from where you install MongoDB (Could be in C:\ or from any other drive where you installed)
- Go to Control Panel >System and Security > System >Advance System Settings> Environmental Variables. There click path option of system variables and hit edit option and add the path of MongoDB (Which you just copied). Environmental variable window displayed below could be different on windows version before windows 10 10586 build. In that case go to path option and hit edit there you will get text box with existing variables there (in text box) add semicolon(;) at the end and paste the path and hit OK.
- Now check weather MongoDB properly installed or not. Start power shell or cmd prompt and type mongo — version. If the result shows the current installed version of MongoDB then we are good to go.
Now let’s create the folder to keep the data. In my case I create it in E:\ drive and named as Mongodb\Empdb. To work with MongoDB you must have to start MongoDB server first weather it’s in remote or in your local machine. To be more clear about mongodb client and server again go back to bin folder of MongoDB you can see mongo and mongod application which is client(mongo) and server (mongod) which talks to each other for operations. To start mongo server in power shell type mongod –dbpath E:\Mongodb\Empdb and hit enter and then mongo server will start and listen the connection on port 27017. E:\Mongodb\Empdb is the path where we will keep our database. Here is how it looks like.
Now let’s see database and create some data on it.
Start another instance of powershell (as a mongo client) and type mongo or mongo [db_name] then it will connect to mongo server and point the the database that you passed (if you try second command). Type show dbs command to see the list of database, by default there will be only localdb. Type db it will create test database. Now to insert data try db.foo.insert(‘name’: ‘janak’) it will insert document (as row of SQL) with details name:Janak in foo collection (Like table of SQL). Collection foo will be automatically create if it does not exist already. To see the data you entered try db.foo.find() you will see your data of that collection (in this command foo) with random object ID (By default it is given by mongo but you can put your own object id to uniquely identify document in collection). For that try db.foo.insert(‘_id’: 19,‘name’: ‘janak’) and see the document by db.foo.find() there you can see 19 as a object id instead of random GUID no. You can see operations logs in another power shell where you run mongodb server earlier.
That’s it for this part. Up to now our MongoDB setup is done. We can connect with mongodb databases through the port 27017 of localhost. In next part I will show you CRUD operation with very simple sample. For this sample we will be using WPF. Till date MongoDB driver is not supported in WinRT application.
Happy Coding !!!