Now it is time to do some fantastic thing, normally connecting to database and retrieving data from database or writing into database is quite interesting if you are working with new platform and tools. Today I want to show you how to connect to the MongoDB and retrieve data (of course I will teach you how to insert first), for a few seconds forget about node.js and focus on MongoDB, in order to create a database in MongoDB we need to follow the below steps:
- MongoDB is an open source database which can be downloaded from the: http://www.mongodb.org . So please open the link and download it. For this tutorial we need MongoDB version Windows 64-bit 2008 R2+
- If you cannot do it please close the website and turn off your pc and do something else as you are not qualified for developing. (Just kidding) if you have difficulty to download or install, please click here to see the video which I uploaded on my YouTube channel.
- After you install, you need to run your MongoDB server, it is very easy.
- Please make sure that you created Data folder in your node folder, I created something like this : E:\node\nodetest1\data
- Now we want to create database and copy necessarily files into the data folder. Open command prompt and go to the MongoDB bin folder, (if you watch video you can see how I moved to bin folder). For those lazy people that do not want watch video you can find Bin folder in this address:
After you moved to this folder key in the below command:
mongod –dbpath E:\node\notetest1\data
Note: dbpath is the address of data folder, so maybe your data folder address is different from mine, please change it accordingly.
Note: In some cases I saw that mongod does not work, so for this case you need to key in the full address of mongod.exe, actually mongod is exe file in bin folder!!!!
Note: it may take a few seconds to a few minutes for command to be finished so please be patient.
- After you ran the command you could see a few files and folders are created in Data folder and also in your command prompt window you can see MongoDB is waiting for connection.
- It means you are in a right direction and your MongoDB is waiting for connection.
- Something like this:
- Be default mongoDB is connected to the test database and even you can see the name of test on your screen once you copy all the necessarily files to the data folder, but do not worry about it, we can always switch to our own database, for this purpose you need to run the MondoShell, to run MondoShell you need to run the below command:
After you press enter you can see something like this:
To change the database you can type this command:
Use [name of you database]
Mongo DB uses Json data structure to keep data, so before we go further I want to show you how Json format looks like:
Basically json format is very simple and understandable for human.
How can we add record to our database?
In order to add records to your database you can use the below command:
In the above command, testcollection is a user defined name for the collection. It means you can change it as you want. This collection looks like and array and keep all the records. Above command must be ran in mongo shell. (Same as step 5).
How can we retrieve records?
In order to retrieve data you can run the below command:
db.testcollection.find ().pretty ()
Then result is something like this:
“_id” : ObjectId(“55d1b2028f5be420d8a167fc”),
“name” : “Mehran”,
“code” : “123”
in the next post I will teach you how to retrieve data from MongoDb and show it Html file by using Node.js.
bye bye 🙂