Search in sources :

Example 6 with DeleteOneModel

use of com.mongodb.client.model.DeleteOneModel in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method toWriteModel.

private WriteModel<BsonDocument> toWriteModel(final BsonDocument document) {
    String requestType = document.getFirstKey();
    BsonDocument arguments = document.getDocument(requestType);
    switch(requestType) {
        case "insertOne":
            return new InsertOneModel<>(arguments.getDocument("document"));
        case "updateOne":
            return arguments.isArray("update") ? new UpdateOneModel<>(arguments.getDocument("filter"), arguments.getArray("update").stream().map(BsonValue::asDocument).collect(toList()), getUpdateOptions(arguments)) : new UpdateOneModel<>(arguments.getDocument("filter"), arguments.getDocument("update"), getUpdateOptions(arguments));
        case "updateMany":
            return arguments.isArray("update") ? new UpdateManyModel<>(arguments.getDocument("filter"), arguments.getArray("update").stream().map(BsonValue::asDocument).collect(toList()), getUpdateOptions(arguments)) : new UpdateManyModel<>(arguments.getDocument("filter"), arguments.getDocument("update"), getUpdateOptions(arguments));
        case "deleteOne":
            return new DeleteOneModel<>(arguments.getDocument("filter"), getDeleteOptions(arguments));
        case "deleteMany":
            return new DeleteManyModel<>(arguments.getDocument("filter"), getDeleteOptions(arguments));
        case "replaceOne":
            return new ReplaceOneModel<>(arguments.getDocument("filter"), arguments.getDocument("replacement"), getReplaceOptions(arguments));
        default:
            throw new UnsupportedOperationException("Unsupported write model type: " + requestType);
    }
}
Also used : InsertOneModel(com.mongodb.client.model.InsertOneModel) BsonDocument(org.bson.BsonDocument) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) BsonString(org.bson.BsonString) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) DeleteManyModel(com.mongodb.client.model.DeleteManyModel)

Aggregations

DeleteOneModel (com.mongodb.client.model.DeleteOneModel)6 ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)5 ArrayList (java.util.ArrayList)5 DeleteManyModel (com.mongodb.client.model.DeleteManyModel)4 InsertOneModel (com.mongodb.client.model.InsertOneModel)4 UpdateOneModel (com.mongodb.client.model.UpdateOneModel)4 UpdateManyModel (com.mongodb.client.model.UpdateManyModel)3 CollectibleCodec (org.bson.codecs.CollectibleCodec)3 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)2 DeleteRequest (com.mongodb.bulk.DeleteRequest)2 InsertRequest (com.mongodb.bulk.InsertRequest)2 UpdateRequest (com.mongodb.bulk.UpdateRequest)2 WriteRequest (com.mongodb.bulk.WriteRequest)2 BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)2 MixedBulkWriteOperation (com.mongodb.operation.MixedBulkWriteOperation)2 BsonDocument (org.bson.BsonDocument)2 BsonString (org.bson.BsonString)2 BsonValue (org.bson.BsonValue)2 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 WriteModel (com.mongodb.client.model.WriteModel)1