Mastering The MongoDB Query Tutorial – Find | Sort | Count Query

Mastering The MongoDB Query Tutorial – Find | Sort | Count Query Tutorial from Coding compilerIn the first chapter, we learn about find. A preliminary understanding of the command. In addition toselectors outsidefind as well as more features. We have already said thatfind the result of the return is onecursor. We will take a closer look at what it means.

MongoDB Query Tutorial – Find | Sort | Count

Let’s start learning about MongoDB queries like find, sort and count queries.

Field selection

Before starting  cursors the topic, you should know that  find there is a second optional parameter called “projection”. This parameter is a list of fields we want to retrieve or exclude. For example, we can just query the name of the returning unicorn without any other fields:

db.unicorns.find({}, {name: 1});

By default, _id fields are always returned. We can explicitly exclude it from the returned result in this way  {name:1, _id: 0}.

Except for  _id fields, you can’t mix and retrieve searches and exclusions. Think about it, this makes sense. You can only explicitly retrieve or exclude certain fields.

MongoDB Sort Query

I have mentioned it many times in the current position,  find returning a cursor, which will only be executed when needed. However, what you see in the shell is that it is  find executed immediately. 

This is just the behavior of the shell. We can find observe cursors the true behavior through a  chained approach  . Let’s take a look  sort. We specify the fields we want to sort, in JSON mode, where 1 means ascending -1 means descending. such as:

//heaviest unicorns first
db.unicorns.find().sort({weight: -1})

//by unicorn name then vampire kills:
db.unicorns.find().sort({name: 1,
    vampires: -1})

Just like a relational database, MongoDB allows you to sort the indexes. We will discuss the index in detail later. That, you should know that MongoDB has a size limit for sorting unindexed fields. 

That is, if you try to sort a very large, unindexed result set, you get an exception. Some people think this is a disadvantage. To be honest, I hope that more databases can have this ability to reject unoptimized queries. (I’m not talking about the disadvantages of each MongoDB as an advantage, but I’ve seen enough databases that lack optimization, and I really hope they have a strict-mode.)

MongoDB Paging

Paging through results can be  limit and  skip to implement cursor method. For example, to get the second and third heavy unicorns, we can do this:

    .sort({weight: -1})

Through the cooperation of the limit sum  sort, you can avoid causing problems when sorting non-index fields.

MongoDB Count Query

The shell can be executed directly on a collection  count like this:

db.unicorns.count({vampires: {$gt: 50}})

In fact, it count ‘s a  cursor way, the shell simply provides a shortcut. This is required when executing in a way that does not provide a shortcut (the same can be done in the shell):

db.unicorns.find({vampires: {$gt: 50}})

Related MongoDB Tutorials

What is MongoDB Tutorial For Beginners

MongoDB Basics Tutorial

MongoDB Update Tutorial

Leave a Comment