MongoDB DBA · Question 374 · Single choice

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:

Answer options

  1. A.
    db.records.createIndex( { "location" : 1, "state": 1 } )
  2. B.
    db.records.createIndex( { "location.city": 1 } )
  3. C.
    db.records.createIndex( { "state": 1 } )
  4. D.
    db.records.createIndex( { "location.state": 1 } )
  5. E.
    db.records.createIndex( { "location": 1 } )

Answer confidence: High

Correct answer: D

Explanation language: Chinese

题干要索引的是嵌套在 location 里的 state 字段,必须用点号路径,所以选 D。C 的 {"state":1} 指向顶层字段,而文档中 state 在 location 子文档内;A、E 索引的是整个 location 子文档(要整个子文档相等才匹配),B 索引的是 city,都无法服务针对 state 的查询。

Papers containing this question

This question appears in 2 bank records. Public pages combine identical prompts and options. Selected questions display the highest-confidence record; the others show a short preview. Paper-based learning preserves each record's answer, explanation, and confidence.

AUTHOR · PROJECTS · OPEN SOURCE

Author & MongoDB projects

Building data platforms, MongoDB migration and sync tools, and cloud operations products.

Contact the author for any MongoDB needs.

Contact the author ↗mlhp123@outlook.com

Support the author

If this question bank helps you, optional donations are welcome.

Scan with Alipay to support the authorOpen full-size donation image ↗