You have the following document in a collection: { "_id": ObjectId("570c04a4ad233577f97dc459"), "team": "Eagles", "location": { "state": "PA", "city": "Philadelphia" } } Which of the following commands would you use to create an index on the "state" field? Choose the best answer:
题目选项
- A.db.records.createIndex( { "location" : 1, "state": 1 } )
- B.db.records.createIndex( { "location.city": 1 } )
- C.db.records.createIndex( { "state": 1 } )
- D.db.records.createIndex( { "location.state": 1 } )
- E.db.records.createIndex( { "location": 1 } )
答案置信度:高
正确答案:D
解析语言:中文
题干要索引的是嵌套在 location 里的 state 字段,必须用点号路径,所以选 D。C 的 {"state":1} 指向顶层字段,而文档中 state 在 location 子文档内;A、E 索引的是整个 location 子文档(要整个子文档相等才匹配),B 索引的是 city,都无法服务针对 state 的查询。