Mongodb queries the difference between a document and a field

  mongodb, question
  1. Mongodb Cluster Replication Set Fragment Deployment Environment. There is a collection device, device and Device has 2 indexes A, B.
  2. There is a query, two kinds of writing
//Time consumption is very stable for 1 ms. Query the content of field B according to index A (B is also an index, and the returned result is 1, with the content being about 45 characters)
 //Unstable 2 ~ 2~2000ms is possible. Query the entire document according to index A (the returned result is 1, and the content is about 600 characters)

Question 1: Find out the whole inquiry process under popular science?
Question 2: The same index is used, and the returned documents are the same. Only the returned contents have some differences in size, but the results are very different. What is the principle behind this?

Additional questions:

@Mongoing Chinese CommunityGreat God, thank you very much for your answer. This question is actuallyMongodb has gone through the index and is still a slow query. Please look at the execution plan.The sequel to the question (you also answered the question. Thank you).

The solution to the previous problem isChange the 2nd query statement to the 1st query statementIn my project, I achieved very good results.
Therefore, I would like to know the direct connection and difference between the two queries and the whole query process.
In addition, can you recommend some good mongodb books (the Chinese version is the best. English is not very good). I envy you for your professionalism and feel that knowledge is really cool.

I don’t know what you mean by the inquiry process. I understand what you want to see is the implementation of the plan.


What is certain is that the query plans of the two queries are almost the same. The second one is one less project stage, but usually this will not have such a big impact. It is probably not the query itself that affects your query time, but the resource status of the server when running these queries. To put it bluntly, there may be other very expensive queries whose execution will affect this query.

Reply to supplementary questions

The two queries do not differ much in essence. Some scenarios may cause performance differences, but not too much.


Whether you take a field or the entire document, what you read on disk is the entire document. Because the document has no structure, how do you know which fields are in it if you don’t read the whole document and analyze it? Therefore, it is impossible to directly read out the contents of a certain field in the current WiredTiger engine (but it is actually in roadmap). I thought about it, too.covered queryThe possibility of, but your writing is obviously can’t do, unless it is:

db.getCollection('device').find({'a':'value'},{'b':1, _id: 0})

It is possible to skipFETCHStage.