- In my previous post(Mongo Database Installation) we have seen Mongo database installation on windows machine.
- In this post, we will see how to use Mongo shell to connect to Mongod service .We will also see how we can create collection and documents in Mongo shell.
- Mongo Shell is like an interface where we can send commands to Mongod service to operate on Mongo Database.
Steps to Connect to Mongod Service:
- Run the Mongod service first,
- Open a new command prompt and connect to the Mongod service,
- Creating/Using a Database collection (using ‘use’ keyword’) ‘studentlibrary‘.If a library of that name is not exists then it will create a new collection. We can see the database used by ‘db’ keyword.
- Inserting documents to ‘studentlibrary’ collection.The collection is ‘math’ here.We have insert methods, It takes JSON format document to save documents in collection.
The documents that we are inserting here are,
{ "name" : "alegbra", "price" : 150}
{ "name" : " trigonometry ", "price" : 153}
{ "name" : " geometry ", "price" : 103}
- Displaying all available documents as compact list inside a collection using find method.there is another method pretty to see the result in good format.
- If you will notice here there is a ‘_id’ keyword automatically generated by Mongo. These are all unique key for each documents.I will explain about this _id generation in a separate blog post.
- We can find a document and projection on results inside a collection using ‘find‘ method by allowing parameters.The 1st parameter is for WHERE condition and 2nd parameter if or PROJECTION.
In the second parameter is also a document type , here we can use true or false in properties to get all result list of document with required properties.