英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论