By Shubham Aggarwal | 1/30/2017 | General |Intermediate

MongoDB Advanced operations

MongoDB Advanced operations

After covering MongoDB basic in the last articles: MongoDB part 1, MongoDB part 2 and MongoDB part 3, we will continue with MongoDB Advanced operations.

In this article, we will have a look at deeper MongoDB operations and build upon our previous articles. This is the fourth article of the series on MongoDB database tutorial, so, do check out the earlier tutorials.

 

Here, we will study about using comparison operators and dangers of replication in MongoDB.

Advanced Operations

We’re putting together a list of the best books we’ve had. Let’s find books with a grade equal to or greater than 80.

Introducing projections

In MongoDB, projection means selecting only the required data rather than selecting whole of the data of a document. If a document has 5 fields and we need to show only 3, then select only 3 fields from the document.

 

find() takes a second parameter called a “projection” that we can use to specify the exact fields we want back by setting their value to true.

 

>>> Note : When selecting fields, all other fields but the _id are automatically set to false.

 

> db.books.find(
 {"grade": {"$gte": 80}},
 {"author": true, "name": true}
)

Result will be:

{
 "_id": ObjectId(...),
 "author": "Hillary",
 "name": "Demons & Angels"
} ...

This way, we can be sure that we extract information we need so database transactions are not heavy and complete when we start to scale.

Excluding fields

By just specifying fields to be false, we can indicate MongoDB to return

 

> db.books.find({"grade": {"$gte": 80}},
 {"author": false, "price": false}
)

Result will be:

{
 "_id": ObjectId(...),
 "name": "Demons & Angels",
 "grade": 94,
 "chapters": [...],
 ...
}

When excluding fields, all fields but those set to false are defaulted to true. Removing fields is a great technique to remove sensitive data like passwords etc.

 

By default, the _id field is always returned whenever selecting or excluding fields. It’s the only field that can be set to false when selecting other fields. We can remove it too,

  

> db.books.find(
 {"grade": {"$gte": 80}},
  {"author": true, "price": true, "_id": false}
)

Now, result will be:

{
 "author": "Hillary",
 "price" : 9.99
}

The only time we can mix an exclusion with selections is when we need to remove the ID field. Removing the id is common when preparing data reports for non-developers.

Either Select or Exclude Fields

Whenever projecting, we either select or exclude the fields we want — we don’t do both.

  

> db.books.find(
 {"grade": {"$gte": 80}},
 {"name": true, "author": false}
)

Causes an error to be raised:

 

"$err": "Can't canonicalize query: BadValue

Projection cannot have a mix of inclusion

and exclusion."

Count number of Books

Time to advertise our expertise and list the total number of books we’ve reviewed. Need to count the total number of books in the store collection.

Introducing cursor

Cursor is defined as “A pointer to the resultset of a query. Clients can iterate through a cursor to retrieve results.”

 

In other words, when we run a query looking for documents, what gets returned is a pointer to the results of that query rather than all of the results in one shot (as you can imagine, this might be impractical and slow for very large result sets).  Essentially, the cursor is the conduit we will use to extract all the results of a query from the database.

 

Now, returning just a pointer with no initial results would be a little inefficient - the first thing we would need to do each time is fetch the first batch of results.  At the same time, if that initial set of results it too large it might take a long time to assemble and transfer to the client.

 

As a compromise, and in the interests of providing a quick response, if our result set is more than 101 documents, MongoDB will return the first 101 documents and then wait for you to ask for more (iterate over the cursor).  If our result set is less than 101 documents, then we get all our results in the initial batch and the cursor is exhausted.

 

This description is a little simplistic, these things are configurable, and there are other limits that might come into play rather than the 101 documents rule, but if we keep the above in mind we will be fine for most situations.

  

> db.books.find({"author": "Hillary"}) {"_id": ObjectId(...), ... }
{"_id": ObjectId(...), ... }
{"_id": ObjectId(...), ... }
…
type “it” for more
// First 20 documents are returned

When there are more than 20 documents, the cursor will iterate through them 20 at a time. Typing “it” will display the next 20 documents in the cursor.

 So,

  

> db.potions.find()

Above command prints 20 elements. Next,

> it

Above command prints next 20 elements. We’ll continue being prompted until no documents are left.

Cursor methods

Since the cursor is actually an object, we can chain methods on it.

 

> db.potions.find().count()

 

find() method returns a cursor and count() return the total document count, which is shown as:

80

Cursor methods always come after find() since it returns the cursor object.

Sort in Cursor

We want to implement a way for users to sort books by price. We can use the sort() cursor method to sort documents.

 

> db.books.find().sort({"price": 1})

 

In above command,

  • -1 to order descending
  • 1 to order ascending

Pagination

Paging through the data is one of the most common operations with MongoDB. A typical scenario is the need to display the results in chunks in our UI. If we are batch processing our data, it is also important to get our paging strategy correct so that our data processing can scale.

 

We only want to show 3 books per page. Time to implement pagination!

 

We can implement basic pagination by limiting and skipping over documents. To do this, we’ll use the skip() and limit() cursor methods.

  

> db.books.find().limit(3)

Above command limit 3 books per page. Since we’re not skipping, we can leave to the skip method and just limit 3.

 

> db.books.find().skip(3).limit(3)

Conclusion

In this article, we had a look at deeper MongoDB operations and build upon our previous articles. This is the fourth article of the series on MongoDB database tutorial, so, do check out the earlier tutorials.


In the next and final part of the series, we will study about MongoDB user profiles, more features in embedded documents and Morphing models.

By Shubham Aggarwal | 1/30/2017 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now