logo-StatusNeo
d

WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

Image Alt

StatusNeo

MongoDB Part-2 (CRUD and Aggregations)

Introduction

  • In our previous part, we have covered the introduction to MongoDB, installation, and hosting a cluster with some basic mongo commands.
  • In this part, we are going to get our hands dirty on CRUD operations and aggregation operations
  • Link to the previous post – https://statusneo.com/mongodb-introduction/
  • The tool that we are going to use for our queries will be Robo3T, and the dataset will be that sample dataset that MongoDB has provided us.

CRUD (Create Read Update Delete) Operations

  • Let’s begin with create operation, we are going to create a collection of students with their name, roll number, class, and email.
  • First, we are going to create a database named as students, in this, we are going to have a collection named as records.

  • Here we can see that one record has been inserted successfully.
  • Now coming to the read operation, the command for that is find.
  • By default find will return all the records.
  • If we want to match something, say name then we are going to pass key and value inside find
  • query will be like db.records.find({“name”:”Karan”})
  • It will return all the records of that match.
  • If we want to limit the response we can add .limit(number) at the end of the query to limit our records.
  • For updating the record, we can use db.records.update({“key”:”value”},{“update”:”record”}) .
  • In update query, we will pass the matching phase in the first part, and the updated record in the next.
  • Update query works as single update, that means only one record will be updated if multiple records of same key are matched.
  • for updating many we need to use update.Many query.
  • In addition to this if we want to add record if it doesn’t exist, then we can use upsert keyword.
  • Query for upsert will be, db.records.update({“find”:”value”},{“new”:”record”},{“upsert”:true})
  • $set keyword can be use to set only specific params while unset is use to remove or unset certain parameters.
  • For using unset { $unset: [ “column1”, “column2” ] }, this query can be update in update query only.
  • Now coming to delete query, delete query can be used as db.records.deleteOne({“key”:”value”}), this will erase only the first matching record.
  • db.records.deleteMany({“key”:”value”}), this will delete all the matching records.

Aggregation Framework

  • The aggregation framework can be called a superior version of the find method.
  • Aggregation framework means creating a pipeline of steps or instructions.
  • These instructions will be run on our data and give our desired output after passing through these pipelines.
  • { $match}, { $group}, { $sort }, { $project} these are the various instructions that can be used while creating the pipeline.
  • In Group, _id is a necessary field. It defines by which key we want to group our data.
  • Every instruction will work on the output of the previous instruction result.
  • Different types of aggregation fields that are available can be seen from here – https://docs.mongodb.com/manual/meta/aggregation-quick-reference/
  • For our demo, we are going to use a sample restaurants database and restaurant collection.
  • Say we want to count the number of restaurants with different cuisines.
  • The query for finding the number of restaurants in the different streets having Asian cuisine.
  • We can pass JSON (document) for _id value if we want to group by multiple values.
  • Then we simply need to pass the key-value pair inside that.
  • $ sign is used when we want to group for a nested field, for example in the below query we group by street.
  • The street was present inside the address as a sub document.
  • For the sort functionality, 1 is used for sorting in the ascending order while -1 is used for sorting in the descending order.
  • For the projection, we can pass the fields we want to project by their name as key and value as 1.
  • By default _id will be projected, if we do not want that in our result we can pass _id : 0 in the in-project JSON.
  • we can also combine multiple fields together in the project JSON.
  • Say we want to have building, street and zipcode in a single key.
  • We can do that using Project JSON.
  • Say we want to get the address of the restaurant named as Riviera Caterer.
  • The challenge is to get the address in only one key.
  • So for that we can use concat function in our project JSON.
  • As you can see from the above query we got our desired result.
  • But we can see that there is no whitespace between the two values.
  • Can we do that?
  • Of course, we can, we just need to pass whitespace in the Concat function.
  • Now suppose we want our address in the uppercase letter only.
  • For that, we can use $toUpper function in our projection, similarly $toLower for making it to lower case.
  • Similarly we can have a particular character or list of character to upper or lower case.
  • This can be done using substr.

Conclusion

  • In this part, we have covered the CRUD operations and the basics of aggregations.
  • In the next part, we will be deep-diving into the aggregation and some more queries.

References

An optimistic Engineer passionate about technology, science, astrology, and photography. Visit this to know more- https://utkarshshukla.herokuapp.com/

Add Comment