PHP 7.4.0 alpha 2 Released


(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.

Liste de param├Ętres


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.

Valeurs de retour


Exemple #1 mysql_xdevapi\Collection::createIndex() example

$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 add a note

User Contributed Notes

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