public class BatchedWrite extends Object implements Serializable
The default mode (BatchedWriteMode.SERIALIZE_AND_CONTINUE
) for this
class is to submit the operations to the server in the order that they are
added to the Builder and to apply as many of the writes as possible (commonly
referred to as continue-on-error). This has the effect of causing the fewest
surprises and optimizing the performance of the writes since the driver can
send multiple distinct writes to the server at once.
The BatchedWriteMode.SERIALIZE_AND_STOP
mode also sends each write as
a separate request but instead of attempting all writes the driver will stop
sending requests once one of the writes fails. This also prevents the driver
from sending multiple write messages to the server which can degrade
performance.
The last mode, BatchedWriteMode.REORDERED
, may re-order writes to
maximize performance. Similar to the
BatchedWriteMode.SERIALIZE_AND_CONTINUE
this mode will also attempt
all writes. The reordering of writes is across all WriteOperationType
s.
If using a MongoDB server after 2.5.5
a batched
write will result in use of the new write commands.
For a more generalized batched write and query capability see the
BatchedAsyncMongoCollection
and MongoCollection.startBatch()
.
Modifier and Type | Class and Description |
---|---|
static class |
BatchedWrite.Builder
Builder for creating
BatchedWrite s. |
static class |
BatchedWrite.Bundle
Bundle is a container for the write command and the
WriteOperation it contains. |
Modifier and Type | Field and Description |
---|---|
static Version |
REQUIRED_VERSION
The first version of MongoDB to support the
aggregate command. |
Modifier | Constructor and Description |
---|---|
protected |
BatchedWrite(BatchedWrite.Builder builder)
Creates a new BatchedWrite.
|
Modifier and Type | Method and Description |
---|---|
static BatchedWrite.Builder |
builder()
Creates a new builder for a
BatchedWrite . |
static BatchedWrite |
delete(DocumentAssignable query,
boolean singleDelete,
Durability durability)
Create a batched write with a single delete operation.
|
Durability |
getDurability()
Returns the durability for the writes.
|
BatchedWriteMode |
getMode()
Returns the mode for submitting the writes to the server.
|
List<WriteOperation> |
getWrites()
Returns the writes to submit to the server.
|
static BatchedWrite |
insert(boolean continueOnError,
Durability durability,
DocumentAssignable... documents)
Create a batched write with a single inserts operation.
|
List<BatchedWrite.Bundle> |
toBundles(String collectionName,
long maxCommandSize,
int maxOperationsPerBundle)
Creates write commands for all of the insert, updates and deletes.
|
static BatchedWrite |
update(DocumentAssignable query,
DocumentAssignable update,
boolean multiUpdate,
boolean upsert,
Durability durability)
Create a batched write with a single update operation.
|
public static final Version REQUIRED_VERSION
aggregate
command.protected BatchedWrite(BatchedWrite.Builder builder)
builder
- The builder for the writes.public static BatchedWrite.Builder builder()
BatchedWrite
.BatchedWrite
.public static BatchedWrite delete(DocumentAssignable query, boolean singleDelete, Durability durability)
MongoCollection.delete(com.allanbank.mongodb.bson.DocumentAssignable)
variants and the driver will convert
the deletes to batched writes as appropriate.
This method avoids the construction of a builder.
query
- The query to find the documents to delete.singleDelete
- If true then only a single document will be deleted. If
running in a sharded environment then this field must be false
or the query must contain the shard key.durability
- The durability of the delete.public static BatchedWrite insert(boolean continueOnError, Durability durability, DocumentAssignable... documents)
MongoCollection.insert(boolean, com.allanbank.mongodb.bson.DocumentAssignable...)
variants and the driver will
convert the inserts to batched writes as appropriate.
This method avoids the construction of a builder.
continueOnError
- If the insert should continue if one of the documents causes
an error.durability
- The durability for the insert.documents
- The documents to add to the collection.public static BatchedWrite update(DocumentAssignable query, DocumentAssignable update, boolean multiUpdate, boolean upsert, Durability durability)
MongoCollection.update(com.allanbank.mongodb.bson.DocumentAssignable, com.allanbank.mongodb.bson.DocumentAssignable)
variants and the driver will convert
the updates to batched writes as appropriate.query
- The query for the update.update
- The update for the update.multiUpdate
- If true then the update will update multiple documents.upsert
- If no document is found then upsert the document.durability
- The durability of the update.public Durability getDurability()
public BatchedWriteMode getMode()
public List<WriteOperation> getWrites()
public List<BatchedWrite.Bundle> toBundles(String collectionName, long maxCommandSize, int maxOperationsPerBundle)
mode
.collectionName
- The name of the collection the documents will be inserted
into.maxCommandSize
- The maximum document size.maxOperationsPerBundle
- The maximum number of writes to include in each bundle.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.