What is a Covered Query?

According to the authority MongoDB documentation, a secured inquiry is a question in which:

  • all the fields in the inquiry are piece of a list and
  • all the fields returned in the inquiry are in the same list

Since all the fields introduce in the inquiry are piece of a list, MongoDB matches the question conditions and furnishes a proportional payback utilizing the same record without really looking inside documents. Since lists are available in RAM, getting information from records is much speedier as contrasted with bringing information by checking documents.

"_id": Objectid("873637402597d85242602983784003"),
"contact": "9170441603",
"dob": "15-07-1995",
"sex": "M",
"name": "mAnsari",
"user_name": "mansari"

We will first make a compound list for clients gathering on field’s sex and user_name utilizing after question:

db.users.ensureindex({gender2,user_name:2})now, this record will blanket the accompanying question:

Our file does exclude _id field, we have expressly rejected it from result set of our question as MongoDB of course returns _id field in every inquiry. So the accompanying question would not have been inside the file made above:


Finally, recollect that a file can’t blanket a question if:

  • any of the listed fields is an exhibit
  • any of the listed fields is a subdocument

MongoDB Analyzing Queries

Analyzing queries is an exceptionally essential part of measuring how successful the database and indexing design is. We will look into the habitually utilized $explain and $hint queries.

Utilizing $explain

The $explain administrator gives data on the inquiry, lists utilized within a question and different detail. It is exceptionally helpful when examining how well your files are enhanced.

"cursor" : "mAnsari gender_1_user_name_1",
"ismultikey" : false,
"n" : 2,
"nscannedobjects" : 1,
"nscanned" : 2,
"nscannedobjectsallplans" : 1,
"nscannedallplans" : 2,
"scanandorder" : false,
"indexonly" : genuine,
"nyields" : 0,
"nchunkskips" : 0,
"millis" : 0,
"indexbounds" : {
"sex" : [
"user_name" : [
"maxattribute" : 2
"$minattribute" : 2

We take a gander at the fields in this result set:
The genuine estimation of indexonly demonstrates that this question has utilized indexing.

READ  MongoDB Text Search and Rockmongo

The cursor field details the kind of cursor utilized. BTreecursor sort shows that a file was utilized furthermore give the name of the list utilized. Basic cursor demonstrates that a full sweep was made without utilizing any records.

  • ndemonstrates the quantity of reports matching returned.
  • nscannedobjects demonstrates the aggregate number of reports checked
  • nscanned demonstrates the aggregate number of reports or file passages filtered

Indexing Array Fields:

Assume that we need to pursuit client records focused around his labels. For this, we will make a record on labels show in the accumulation.

Making a file on exhibit thusly makes separate list sections for each of its fields. So for our situation when we make a file on labels exhibit, separate files will be made for its values music, cricket and sites.

To make a file on labels exhibit, utilize the accompanying code:


In the wake of making the file, we can seek on the labels field of the gathering like this:


To check that fitting indexing is utilized, utilize the accompanying explain charge:


The above explain summon brought about “cursor” : “Btreecursor tags_1” which affirms that fitting indexing is utilized.

Indexing Sub-Document Fields:

Assume that we need to inquiry records focused around city, state and pincode fields. Since all these fields are some piece of location sub-report field, we will make record on all the fields of the sub-archive.

Creation Timestamp of a Document

Since the _id Objectid of course stores the 4 byte timestamp, by and large you don’t have to store the creation time of any archive. You can bring the creation time of an archive utilizing gettimestamp strategy:


This will give back where it’s due time of this archive in ISO Date form: