MongoDB Fundamentals – CRUD: Reading Objects – Day 3 of 6

This entire week, I am going to discuss the Fundamentals of MongoDB. Today is the third post in the six post series. We are going to learn about CRUD Operation: Reading Objects in MongoDB. I have recently build a Video Course at Foundations of Document Databases with MongoDB focusing on helping users to get started with MongoDB quickly and efficiently.

MongoDB Fundamentals - CRUD: Reading Objects - Day 3 of 6 ReadOperation-800x380

CRUD Operation: Read Operation

CRUD is an acronym and it stands for Create, Read, Update, and Delete. MongoDB is a NoSQL database and that means along with SQL concepts, it is also following NoSQL concepts.

For MongoDB Read operations actually stands for reading documents from the collection.

Here are the most popular read methods in MongoDB.

db.collection.find() – Inserts a single document in the collection.

If you just run the above command, it will read all the documents in the current collection.

Reading Objects

MongoDB reads documents from the collection and it does it pretty fast as per my experience. When I initially started with the schema-less design, I had so many concerns like what happens if we try to retrieve a field that does not exist on the collection but here is where MongoDB is a document-based database, works differently. It actually will not give you an error but silently will say there is no document that matches the field you are looking for – an entirely different way to look at the problem and its solution compared to the relational world.

Finding Documents

Let us understand some of the keywords which will help us retrieve documents from our collection.

We will be using the find() method to retrieve our documents. It takes two parameters. The first parameter is the query and the second parameter is projection.

lection.find(query, projection)

Query – it is essentially filter for the collection.

Project – it is essentially fields to include in the results.

Here are a few sample commands to read data from the collections named movie.

List all the movies:

db.movies.find( {} )

List all the movies and prettify the result

db.movies.find( {} ).pretty()

List all the movies and with runtime equal to 100 minutes.

db.movies.find( {runtime: 100} ).pretty()

List any 5 movies with runtime equal to 100 minutes.

db.movies.find( {runtime: 100} ).pretty().limit(5)

List any 5 movie titles and runtime with a runtime greater than 100 minutes.

db.movies.find( {runtime: {$eq: 100}}, {runtime:1, title:1, _id:0} ).pretty().limit(5)

List top 5 movie titles and runtime with a runtime greater than 100 minutes, ordered by movie title descending.

db.movies.find( {runtime: {$eq: 100}}, {runtime:1, title:1, _id:0} ).pretty().limit(5).sort({title: -1})

I hope this example would give you an idea of how we can keep on suffixing new operators at the end of the original operator and get the necessary results.

While MongoDB may look confusing in the beginning once you start reading objects, you will start seeing how easy and flexible it is to use it with various business scenarios.

Learning

Here are some of the relevant blog posts on MongoDB.

Technology Online

I hope you find these Learning paths helpful. If you have a Pluralsight subscription, you can watch it for free. If you do not have a Pluralsight subscription, you can still watch the course for FREE by signing up for a trial account. Please note that you do not need any credit card. You can always connect with me on twitter.

Reference: Pinal Dave (https://blog.sqlauthority.com)

, , ,
Previous Post
MongoDB Fundamentals – CRUD: Creating Objects – Day 2 of 6
Next Post
MongoDB Fundamentals – CRUD: Updating Objects – Day 4 of 6

Related Posts

Leave a Reply

Menu