Top Description Methods
com.mysql.cj.xdevapi

public Interface Collection

extends DatabaseObject
Known Direct Implementers
com.mysql.cj.xdevapi.CollectionImpl
Imports
java.util.Map

Representation of a document collection. This interface allows access to and manipulation of the collection through add/find/modify/remove statements.

Method Summary

Modifier and TypeMethod and Description
public AddStatement

Returns:

AddStatement
add
(Map<String, ?>
map of key-value parameters representing the document fields
doc
)

Add a document in the form of a Map.

public AddStatement

Returns:

AddStatement
add
(String...
one or more documents given as JSON strings
jsonStrings
)

Add one or more documents.

public AddStatement

Returns:

AddStatement
add
(DbDoc document)

Add a document in the form of a DbDoc.

public AddStatement

Returns:

AddStatement
add
(DbDoc...
one or more documents given as DbDoc
documents
)

Add a sequence of documents.

public Result

Returns:

Result object, which will indicate the number of affected documents (0 - if none, 1 - if added, 2 - if replaced)
addOrReplaceOne
(String
the document id of the document to be replaced
id
,
DbDoc
the new document, which may contain expressions. If doc contains an _id value and it does not match the given id then the error will be thrown.
doc
)

Adds the document to the collection.

public Result

Returns:

Result object, which will indicate the number of affected documents (0 - if none, 1 - if added, 2 - if replaced)
addOrReplaceOne
(String
the document id of the document to be replaced
id
,
String
the new document, given as JSON string, which may contain expressions. If doc contains an _id value and it does not match the given id then the error will be thrown.
jsonString
)

Adds the document to the collection.

public long

Returns:

The number of documents in this collection
count
()

Query the number of documents in this collection.

public Result

Returns:

Result
createIndex
(String
index name
indexName
,
DbDoc
JSON document with the following fields:
  • fields: array of IndexField objects, each describing a single document member to be included in the index (see below)
  • type: string, (optional) the type of index. One of INDEX or SPATIAL (case insensitive). Default is INDEX and may be omitted.
where 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 (see below for a list). For numeric types, the optional UNSIGNED keyword may follow. For the TEXT type, the length to consider for indexing may be added. Type descriptions are case insensitive.
  • 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: int, (optional) special option flags for use when decoding GEOJSON data
  • srid: int, (optional) srid value for use when decoding GEOJSON data
indexDefinition
)

Create a new statement defining the creation of an index on this collection.

public Result

Returns:

Result
createIndex
(String
index name
indexName
,
String
JSON document with the following fields:
  • fields: array of IndexField objects, each describing a single document member to be included in the index (see below)
  • type: string, (optional) the type of index. One of INDEX or SPATIAL. Default is INDEX and may be omitted.
where 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 (see below for a list). 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: int, (optional) special option flags for use when decoding GEOJSON data
  • srid: int, (optional) srid value for use when decoding GEOJSON data
jsonIndexDefinition
)

Create a new statement defining the creation of an index on this collection.

public void
dropIndex(String
index name
indexName
)

Create a new statement defining the removal of an index on this collection.

public FindStatement

Returns:

FindStatement
find
()

Create a new find statement retrieving all documents in the collection.

public FindStatement

Returns:

FindStatement
find
(String
condition expression
searchCondition
)

Create a new find statement retrieving documents matching the given search condition.

public DbDoc

Returns:

the document, or NULL if no match found
getOne
(String
the document id of the document to be retrieved
id
)

Return the document with the given id.

public ModifyStatement
modify(String
condition expression
searchCondition
)

Create a new modify statement affecting documents matching the given search condition.

public DbDoc

Returns:

DbDoc
newDoc
()

Create a new document.

public RemoveStatement
remove(String
condition expression
searchCondition
)

Create a new removal statement affecting documents matching the given search condition.

public Result

Returns:

Returns a Result object, which will indicate the number of removed documents (1 or 0, if none)
removeOne
(String
the document id of the document to be removed
id
)

Removes the document with the given id.

public Result

Returns:

Result object, which will indicate the number of affected documents (1 or 0, if none)
replaceOne
(String
the document id of the document to be replaced
id
,
DbDoc
the new document, which may contain expressions. If document contains an _id value, it is ignored.
doc
)

Takes in a document object that will replace the matching document.

public Result

Returns:

Result object, which will indicate the number of affected documents (1 or 0, if none)
replaceOne
(String
the document id of the document to be replaced
id
,
String
the new document, given as JSON string, which may contain expressions. If document contains an _id value, it is ignored.
jsonString
)

Takes in a document object that will replace the matching document.

Inherited from com.mysql.cj.xdevapi.DatabaseObject:
existsInDatabasegetNamegetSchemagetSession

Method Detail

addback to summary
public AddStatement add(Map<String, ?> doc)

Add a document in the form of a Map.

Parameters
doc:Map<String, ?>

map of key-value parameters representing the document fields

Returns:AddStatement

AddStatement

addback to summary
public AddStatement add(String... jsonStrings)

Add one or more documents.

Parameters
jsonStrings:String[]

one or more documents given as JSON strings

Returns:AddStatement

AddStatement

addback to summary
public AddStatement add(DbDoc document)

Add a document in the form of a DbDoc.

Parameters
document:DbDoc

DbDoc

Returns:AddStatement

AddStatement

addback to summary
public AddStatement add(DbDoc... documents)

Add a sequence of documents.

Parameters
documents:DbDoc[]

one or more documents given as DbDoc

Returns:AddStatement

AddStatement

addOrReplaceOneback to summary
public Result addOrReplaceOne(String id, DbDoc doc)

Adds the document to the collection. The following algorithm applies:

Parameters
id:String

the document id of the document to be replaced

doc:DbDoc

the new document, which may contain expressions. If doc contains an _id value and it does not match the given id then the error will be thrown.

Returns:Result

Result object, which will indicate the number of affected documents (0 - if none, 1 - if added, 2 - if replaced)

addOrReplaceOneback to summary
public Result addOrReplaceOne(String id, String jsonString)

Adds the document to the collection. The following algorithm applies:

Parameters
id:String

the document id of the document to be replaced

jsonString:String

the new document, given as JSON string, which may contain expressions. If doc contains an _id value and it does not match the given id then the error will be thrown.

Returns:Result

Result object, which will indicate the number of affected documents (0 - if none, 1 - if added, 2 - if replaced)

countback to summary
public long count()

Query the number of documents in this collection.

Returns:long

The number of documents in this collection

createIndexback to summary
public Result createIndex(String indexName, DbDoc indexDefinition)

Create a new statement defining the creation of an index on this collection.

Example: collection.createIndex("myIndex", "{\"fields\": [{\"field\": \"$.myGeoJsonField\", \"type\": \"GEOJSON\", \"required\": true, \"options\": 2, \"srid\": 4326}], \"type\":\"SPATIAL\"}");

Parameters
indexName:String

index name

indexDefinition:DbDoc

JSON document with the following fields:

  • fields: array of IndexField objects, each describing a single document member to be included in the index (see below)
  • type: string, (optional) the type of index. One of INDEX or SPATIAL (case insensitive). Default is INDEX and may be omitted.
where 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 (see below for a list). For numeric types, the optional UNSIGNED keyword may follow. For the TEXT type, the length to consider for indexing may be added. Type descriptions are case insensitive.
  • 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: int, (optional) special option flags for use when decoding GEOJSON data
  • srid: int, (optional) srid value for use when decoding GEOJSON data
Returns:Result

Result

createIndexback to summary
public Result createIndex(String indexName, String jsonIndexDefinition)

Create a new statement defining the creation of an index on this collection.

Example: collection.createIndex("myIndex", "{\"fields\": [{\"field\": \"$.myGeoJsonField\", \"type\": \"GEOJSON\", \"required\": true, \"options\": 2, \"srid\": 4326}], \"type\":\"SPATIAL\"}");

Parameters
indexName:String

index name

jsonIndexDefinition:String

JSON document with the following fields:

  • fields: array of IndexField objects, each describing a single document member to be included in the index (see below)
  • type: string, (optional) the type of index. One of INDEX or SPATIAL. Default is INDEX and may be omitted.
where 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 (see below for a list). 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: int, (optional) special option flags for use when decoding GEOJSON data
  • srid: int, (optional) srid value for use when decoding GEOJSON data
Returns:Result

Result

dropIndexback to summary
public void dropIndex(String indexName)

Create a new statement defining the removal of an index on this collection.

Parameters
indexName:String

index name

findback to summary
public FindStatement find()

Create a new find statement retrieving all documents in the collection.

Returns:FindStatement

FindStatement

findback to summary
public FindStatement find(String searchCondition)

Create a new find statement retrieving documents matching the given search condition.

Parameters
searchCondition:String

condition expression

Returns:FindStatement

FindStatement

getOneback to summary
public DbDoc getOne(String id)

Return the document with the given id.

Parameters
id:String

the document id of the document to be retrieved

Returns:DbDoc

the document, or NULL if no match found

modifyback to summary
public ModifyStatement modify(String searchCondition)

Create a new modify statement affecting documents matching the given search condition.

Parameters
searchCondition:String

condition expression

Returns:ModifyStatement

ModifyStatement

newDocback to summary
public DbDoc newDoc()

Create a new document.

Returns:DbDoc

DbDoc

removeback to summary
public RemoveStatement remove(String searchCondition)

Create a new removal statement affecting documents matching the given search condition.

Parameters
searchCondition:String

condition expression

Returns:RemoveStatement

RemoveStatement

removeOneback to summary
public Result removeOne(String id)

Removes the document with the given id.

Parameters
id:String

the document id of the document to be removed

Returns:Result

Returns a Result object, which will indicate the number of removed documents (1 or 0, if none)

replaceOneback to summary
public Result replaceOne(String id, DbDoc doc)

Takes in a document object that will replace the matching document. If no matches are found, the function returns normally with no changes being made.

Parameters
id:String

the document id of the document to be replaced

doc:DbDoc

the new document, which may contain expressions. If document contains an _id value, it is ignored.

Returns:Result

Result object, which will indicate the number of affected documents (1 or 0, if none)

replaceOneback to summary
public Result replaceOne(String id, String jsonString)

Takes in a document object that will replace the matching document. If no matches are found, the function returns normally with no changes being made.

Parameters
id:String

the document id of the document to be replaced

jsonString:String

the new document, given as JSON string, which may contain expressions. If document contains an _id value, it is ignored.

Returns:Result

Result object, which will indicate the number of affected documents (1 or 0, if none)