Subject: [mongodb-user] Re: Projection low-level mechanism



Hi,

Is in this way that, a document is fetched into RAM entirely, and the required fields are selected to return?

That is correct for a typical db.collection.find() queries with one exception (which I’ll get into later). You can confirm this behaviour using the db.collection.explain() results of a query with projection.

For example, let’s say we have a test collection of documents with the shape: {_id:<...>, a:<...>, b:<...>} and an index of {a: 1}. Using MongoDB 3.4.6, running db.test.explain().find({a: {$lte:10}}, {a:1, b:1}) results in:

> db.test.explain().find({a:{$lte:10}}, {a:1, b:1})
{
  "queryPlanner": {
   ....
    "parsedQuery": {
      "a": {
        "$lte": 10
      }
    },
    "winningPlan": {
      "stage": "PROJECTION",
      "transformBy": {
        "a": 1,
        "b": 1
      },
      "inputStage": {
        "stage": "FETCH",
        "inputStage": {
          "stage": "IXSCAN",
          "keyPattern": {
            "a": 1
          },
          ....

There are more information regarding how to read the explain results in the explain results page, but in short, you read them from inside to outside, with the output of the inner stage becomes the input of the outer stage. Using this rule of thumb, the winning plan shows a procession of: IXSCAN → FETCH → PROJECTION stages. In other words, MongoDB did an index scan to determine the relevant documents, then fetch those documents from disk into RAM, then perform projection on the loaded documents.

The one exception I mentioned earlier is about covered queries. In covered queries, MongoDB can return documents without fetching them from disk if all of the projection fields exist in the index. Using the previous example, we have an index {a: 1}. Therefore a query which specifies a projection of only the a field will be covered by the {a: 1} index. Such a projection would be {_id:0, a:1}. The explain output of that query is:

> db.test.explain().find({a:{$lte:10}}, {_id:0, a:1})
{
  "queryPlanner": {
   ....
    "parsedQuery": {
      "a": {
        "$lte": 10
      }
    },
    "winningPlan": {
      "stage": "PROJECTION",
      "transformBy": {
        "_id": 0,
        "a": 1
      },
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "a": 1
        },
        ....

Note that the procession of stages is now IXSCAN → PROJECTION. In this covered query, if your indexes can fit in RAM, MongoDB can return the query results using only the index, skipping the FETCH stage altogether.

Best regards,
Kevin

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/7c998b9f-edf4-4616-88cb-5d249bd278ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Programming list archiving by: Enterprise Git Hosting