MongoDB索引需要包括”_id”才能覆盖在投影”_id”时吗?

huangapple go评论80阅读模式
英文:

MongoDB index need to include "_id" for it to cover if projecting "_id"?

问题

根据MongoDB Covered查询文档:链接,对于索引 db.inventory.createIndex( { type: 1, item: 1 } ),投影必须明确排除 "_id" 以使索引覆盖查询。但是索引必须包含 "_id" 才能与原始文档关联。有人知道为什么吗?

期望在索引中不包括 _id,因为它不在查询中使用,而是需要在输出中使用。

英文:

According to MongoDB Covered query documentation: Link ,
for an index db.inventory.createIndex( { type: 1, item: 1 } )
projection has to explicitly exclude "_id" for an index to cover the query.
But Index has to have the "_id " for it to tie back to original document. Does someone know why?

Expecting not to include _id in index as thats not queried on but rather need it in output.

答案1

得分: 0

"但索引必须具有“_id”才能与原始文档关联" 这一说法是不正确的。在内部,MongoDB 使用 $recordId 字段来跟踪和引用文档。另请参阅 .showRecordId() 文档

因此,默认情况下,_id 字段不包含在索引中。如果您希望将该信息返回给客户端(或者筛选它等等),则必须在索引定义中包含它。这就是您所引用文档中的指导原因。

英文:

>But Index has to have the "_id " for it to tie back to original document.

This claim is not correct. Internally MongoDB uses a $recordId field to track and refer to documents. See also the .showRecordId() documentation.

As a result, the _id field is not included in an index by default. If you would like that information returned to the client (or to filter on it etc.), then you must include it in the index definition. This is the reason for the guidance in the documentation that you referenced.

huangapple
  • 本文由 发表于 2023年5月20日 23:05:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定