Class Insert (3.53.0)

public final class Insert extends Stage

A pipeline stage that inserts each document produced by the previous stages as a new document.

The insert fails with ALREADY_EXISTS if a target document already exists.

The target path of each document is determined as follows:

  • With neither a collection nor a document ID expression, the input document's own path (its name field) is used, so inserting documents read from the database fails with ALREADY_EXISTS, and a document without a name field fails with INVALID_ARGUMENT.
  • With only a collection, the document is written to that collection under the input document's ID, or under a generated ID if the input document has no name.
  • With only a document ID expression, the document is written to the input document's parent collection under the ID that the expression evaluates to.
  • With both, the document is written to the collection under the ID that the expression evaluates to.

Example:


 // Copy a document into another collection under a new ID.
 firestore.pipeline()
     .collection("books")
     .where(field("title").equal("Dune"))
     .insert(new Insert().withCollection("archive").withDocumentIdExpression(constant("dune")))
     .execute()
     .get();
 

See Also: com.google.cloud.firestore.Pipeline#insert(Insert)

Inheritance

java.lang.Object > Stage > Insert

Constructors

Insert()

public Insert()

Creates an insert stage with no target collection and no document ID expression, so each document is inserted at the input document's own path.

Methods

withCollection(String collectionPath)

public Insert withCollection(String collectionPath)

Sets the collection that documents are inserted into.

Parameter
Name Description
collectionPath String

The path of the target collection, for example "books" or "users/alice/books".

Returns
Type Description
Insert

A new Insert stage with the target collection set.

withDocumentIdExpression(Expression documentIdExpression)

public Insert withDocumentIdExpression(Expression documentIdExpression)

Sets the expression that produces the ID of each inserted document.

Parameter
Name Description
documentIdExpression Expression

An expression that is evaluated against each input document and must evaluate to a document ID (not a document path). Use constant("id") for a fixed ID.

Returns
Type Description
Insert

A new Insert stage with the document ID expression set.