(No version information available, might only be in Git)

Collection::createIndexCreate collection index


public mysql_xdevapi\Collection::createIndex(string $index_name, string $index_desc_json): void

Creates an index on the collection.

An exception is thrown if an index with the same name already exists, or if index definition is not correctly formed.

Bağımsız Değişkenler


The name of the index that to create. This name must be a valid index name as accepted by the CREATE INDEX SQL query.


Definition of the index to create. It contains an array of IndexField objects, and each object describes a single document member to include in the index, and an optional string for the type of index that might be INDEX (default) or SPATIAL.

A single IndexField description consists of the following fields:

  • field: string, the full document path to the document member or field to be indexed.

  • type: string, one of the supported SQL column types to map the field into. For numeric types, the optional UNSIGNED keyword may follow. For the TEXT type, the length to consider for indexing may be added.

  • required: bool, (optional) true if the field is required to exist in the document. Defaults to false, except for GEOJSON where it defaults to true.

  • options: integer, (optional) special option flags for use when decoding GEOJSON data.

  • srid: integer, (optional) srid value for use when decoding GEOJSON data.

It is an error to include other fields not described above in IndexDefinition or IndexField documents.

Dönen Değerler


Örnek 1 mysql_xdevapi\Collection::createIndex() example

= mysql_xdevapi\getSession("mysqlx://user:password@localhost");
$session->sql("DROP DATABASE IF EXISTS addressbook")->execute();
$session->sql("CREATE DATABASE addressbook")->execute();

$schema = $session->getSchema("addressbook");
$collection = $schema->createCollection("people");

// Creating a text index
'{"fields": [{
"field": "$.name",
"type": "TEXT(25)",
"required": true}],
"unique": false}'

// A spatial index
'{"fields": [{
"field": "$.home",
"type": "GEOJSON",
"required": true}],
"type": "SPATIAL"}'

// Index with multiple fields
'{"fields": [
"field": "$.name",
"type": "TEXT(20)",
"required": true
"field": "$.age",
"type": "INTEGER"
"field": "$.job",
"type": "TEXT(30)",
"required": false
"unique": true
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top