public final class Insert extends StageA 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
namefield) is used, so inserting documents read from the database fails withALREADY_EXISTS, and a document without anamefield fails withINVALID_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)
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 |
StringThe path of the target collection, for example |
| Returns | |
|---|---|
| Type | Description |
Insert |
A new |
withDocumentIdExpression(Expression documentIdExpression)
public Insert withDocumentIdExpression(Expression documentIdExpression)Sets the expression that produces the ID of each inserted document.
| Parameter | |
|---|---|
| Name | Description |
documentIdExpression |
ExpressionAn expression that is evaluated against each input document and
must evaluate to a document ID (not a document path). Use |
| Returns | |
|---|---|
| Type | Description |
Insert |
A new |