[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["\u003cbr /\u003e\n\n\n|--------------------------------------------------------|\n| *Relevant to Cloud Firestore Enterprise edition only.* |\n\n\u003cbr /\u003e\n\nThis page describes indexing for Cloud Firestore with MongoDB compatibility.\nCloud Firestore with MongoDB compatibility does not create any indexes by default. To improve database\nperformance, create indexes for your most commonly used queries.\n\nIndexes have a large impact on the performance of a database. If an index exists\nfor a query, the database can efficiently return results by reducing the amount\nof data that needs to be scanned and reducing the work needed to sort the\nresults. However, index entries increase storage costs and the amount of work\ndone during a write operation on indexed fields.\n\nIndex definition and structure\n\nAn index consists of the following:\n\n- a collection ID\n- a list of fields in the given collection\n- an order, either ascending or descending, for each field\n\nAn index can also enable the [sparse](#sparse_indexes), [multikey](#multikey), or [unique](#unique_indexes) options.\n\nIndex ordering\n\nThe order and sort direction of each field uniquely defines the index. For\nexample, the following indexes are two distinct indexes and not\ninterchangeable:\n\n| Collection | Fields |\n|------------|-----------------------------------------------|\n| cities | country (ascending), population (descending) |\n| cities | population (descending), country (ascending), |\n\nWhen creating an index to support a query, include the fields in the same order\nas your query.\n\nIndex density\n\nBy default, index entries store data from all documents in a collection. This is\nknown as a non-sparse index. An index entry will be added for a document regardless\nof whether the document contains any of the fields specified in the index.\nNon-existent fields are treated as having a NULL value when generating index\nentries. To change this behavior, you can define the index as a sparse index.\n\nSparse indexes\n\nA sparse index indexes only the documents in the\ncollection that contain a value (including null) for at least one of the indexed\nfields. A sparse index reduces storage costs and can improve performance.\n\nMultikey indexes for array values\n\nIf you are creating an index on a field that contains array values, you must\ncreate a multikey index. A regular index cannot\nindex array values. A multikey index supports up to one array field in the\nindex definition and can be used for operations that traverse array values.\n\nOnly use multikey indexes if you know that you need to index array values.\nRegular indexes have advantages when processing a query. For example, regular\nindexes can filter values within a range more efficiently.\n\nThe following situations lead to errors when working with array values and\nmultikey indexes:\n\n- An operation attempts to add an array value to a field indexed by a regular index. To add the array value, you must delete existing regular indexes on that field, and recreate them as multikey indexes.\n- You attempt to create a regular index on a field that contains an array value. You must either create a multikey index or delete the array values.\n- An operation attempts to index multiple fields with array values. You cannot have more than one field with an array value in a multikey index. To proceed, modify your data model or your index definitions.\n- You attempt to create a multikey index where two field paths share a common prefix like `users.posts` and `users.zip`.\n\nUnique indexes\n\nSet the unique index option to enforce unique values for the indexed fields.\nFor indexes on multiple fields, each combination of values must be unique\nacross the index. The database rejects any update and insert operations that\nattempt to create index entries with duplicate values. If the data\nof the indexed fields contains duplicate values and you attempt\nto create a unique index, then the index build fails with an error message\nin the operation details.\n\nAbsent fields in a unique index\n\nIf you insert a document with missing fields for the unique index, the index\nsets `null` values for the missing fields. The resulting index entry must be\nunique or the operation fails.\n\nFor example, with this index: \n\n```bash\ndb.cities.createIndex( { \"name\": 1 }, { unique: true } )\n```\n\nIf you add the document `{\"abbreviation\": \"LA\"}` to the collection, the unique\nindex creates an entry with `name` set to `null`. If you then try to add the\ndocument `{\"abbreviation\": \"NYC\"}`, the operation fails because the resulting\nentry for the unique index is the same.\n\nThe same behavior applies to unique indexes with multiple fields.\nWhen creating or updating a document, missing\nindexed fields are set to `null` and the resulting index entry must be\nunique in the index.\n\nTroubleshoot index building errors\n\nYou might encounter index building errors when managing your indexes. An\nindexing operation can fail if the database encounters a problem with the data.\nIndexing operations can fail for the following reasons:\n\n- You have reached an index limit. For example, the operation may have reached the maximum number of index entries per document. If index creation fails, you see an error message. If you have not reached an index limit, retry the index operation.\n- A multikey index is required. At least one of the indexed fields contains an array value. To proceed, you must either use a multikey index or delete the array values.\n- An operation attempts to index multiple fields with array values. You cannot have more than one field with an array value in a multikey index. To proceed, modify your data model or your index definitions.\n- You set the unique index option and the data of the indexed fields would create duplicate index entries. To proceed, remove duplicate combinations of values from the data.\n\n| **Warning:** An ongoing index building error might impact creation of new indexes. Resolving the errors before creating indexes under the same collection.\n\nWhat's next\n\n- Learn how to [create and manage indexes](/docs/firestore/enterprise/indexing)"]]