In the recent articles, we’ve seen what MongoDB is and how to install MongoDB on Windows. We’ve also evaluated the best available GUI tools to use along with your MongoDB database instance. With all these details, let’s get started with one of the in-action sessions on how to query / update and also work around with the data on retrieval. Let us get started with that, no further delay.
Let’s take this in a step-by-step process – there are 5 steps to this process. First is to create a database of our choice, the second step is to create a collection under the database that we have created. Next step is to create a set of documents under the collection that we have created. Next step is to query on the documents set of the collection and then comes the last leg, sort the documents set as per our wish.
The command to use to create a database in MongoDB database is used. The command when used along with a database name, will create the database if it doesn’t exist and if exists returns the database.
Let’s try creating a database with name ‘SampleSortExampleDB’.
A point to note here is that command show dbs will not list your database as existing without at least one record inserted into it. Hence to check whether the database is created or not, you may want to check it with the same command as above – which ensures that the database exists.
The command to use to create a collection in the database that we have just created in the step above is as follows:
name is the name of your new Collection that you are going to create.
options is a document to specify the configuration of your collection, which is an optional parameter
1. capped – If mentioned true then it creates a capped collection (a fixed size collection which overwrites the oldest entries on reaching the size value mentioned)
2. autoIndexId – Default is false, but when marked true it indexes on _idfields.
3. size – mentions the maximum size of a capped collection (mandatory if capped is true)
4. max – mentions the number of documents allowed in a capped collection.
For our example, let us proceed with an ordinary collection, screenshot follows:
The commands to use in MongoDB to create a record / document on the database is either update() or save(). If we try to run the insert command without creating the collection, then MongoDB creates the collection first and then it inserts the document as per the command. If you want to insert more than one document in a single insert query then you can pass all the details in an array separated by commas.
The basic syntax for inserting a document into a collection is as follows:
Let’s take a look at an example on how to insert a document into our MongoDB collection that we have created in the step above. Screenshot follows:
For the ease of use, provided the syntax for creating a document in the collection along with the screenshot as well:
Let’s now see how we can update an existing document via MongoBooster GUI tool. Use GUI tool or not, the commands to achieve will be the same on a MongoDB shell or the GUI tool. Hence this should be just fine to use either the MongoDB shell or the GUI tool.
Now that we are able to insert and update documents via the commands above, let us try to query the MongoDB database and the collection for a specific set of documents out of the existing set of documents. For this, we have created a set of documents in the collection just like as it’s show above.
There are many methods available for querying the documents on MongoDB database / collection - let’s take two examples on how to use them on two different use cases, screenshots with the syntax follows:
find() method requires on what column you want to query your collection about and based on that the matching result set will be displayed for us to choose from. findOne() method again requires the same but here that the query result should be unique, _id will be returned in the projection even if it is not selected as part of the select list or something. The _id is a 12 byte hexadecimal number that is unique for every document that gets inserted into a collection of a MongoDB database. This is by default the design of MongoDB database.
Now with the brief introduction into the basic MongoDB database operations like INSERT, UPDATE, and QUERY – we are now on the last leg on sorting the documents as per the JSON structure that we have defined for our documents in the collection.
For easier explanation, let me show just the fields on which we will sort – so that the data and the sort() function can be understood better.
Screenshots with the commands on how to sort (in the descending order based on ‘documentTitle’ field) is as follows:
Screenshots with the commands on how to sort (in the ascending order based on ‘documentTitle’ field) is as follows:
Using the MongoDB shell is next to easy, if you know the commands to use. If you find it difficult to use the MongoDB shell all by yourself, then you can completely rely on any of the GUI tools for your OS type. For Windows, we suggest that you use MongoBooster GUI tool for all your operations on MongoDB database.
Free Demo for Corporate & Online Trainings.