Search in sources :

Example 1 with DeleteResult

use of com.mongodb.client.result.DeleteResult in project mongo-java-driver by mongodb.

the class GridFSTest method arrangeGridFS.

private void arrangeGridFS(final BsonDocument arrange) {
    if (arrange.isEmpty()) {
        return;
    }
    for (BsonValue fileToArrange : arrange.getArray("data", new BsonArray())) {
        final BsonDocument document = fileToArrange.asDocument();
        if (document.containsKey("delete") && document.containsKey("deletes")) {
            for (BsonValue toDelete : document.getArray("deletes")) {
                final BsonDocument query = toDelete.asDocument().getDocument("q");
                int limit = toDelete.asDocument().getInt32("limit").getValue();
                final MongoCollection<BsonDocument> collection;
                if (document.getString("delete").getValue().equals("fs.files")) {
                    collection = filesCollection;
                } else {
                    collection = chunksCollection;
                }
                if (limit == 1) {
                    new MongoOperation<DeleteResult>() {

                        @Override
                        public void execute() {
                            collection.deleteOne(query, getCallback());
                        }
                    }.get();
                } else {
                    new MongoOperation<DeleteResult>() {

                        @Override
                        public void execute() {
                            collection.deleteMany(query, getCallback());
                        }
                    }.get();
                }
            }
        } else if (document.containsKey("insert") && document.containsKey("documents")) {
            if (document.getString("insert").getValue().equals("fs.files")) {
                new MongoOperation<Void>() {

                    @Override
                    public void execute() {
                        filesCollection.insertMany(processFiles(document.getArray("documents"), new ArrayList<BsonDocument>()), getCallback());
                    }
                }.get();
            } else {
                new MongoOperation<Void>() {

                    @Override
                    public void execute() {
                        chunksCollection.insertMany(processChunks(document.getArray("documents"), new ArrayList<BsonDocument>()), getCallback());
                    }
                }.get();
            }
        } else if (document.containsKey("update") && document.containsKey("updates")) {
            final MongoCollection<BsonDocument> collection;
            if (document.getString("update").getValue().equals("fs.files")) {
                collection = filesCollection;
            } else {
                collection = chunksCollection;
            }
            for (BsonValue rawUpdate : document.getArray("updates")) {
                final BsonDocument query = rawUpdate.asDocument().getDocument("q");
                final BsonDocument update = rawUpdate.asDocument().getDocument("u");
                update.put("$set", parseHexDocument(update.getDocument("$set")));
                new MongoOperation<UpdateResult>() {

                    @Override
                    public void execute() {
                        collection.updateMany(query, update, getCallback());
                    }
                }.get();
            }
        } else {
            throw new IllegalArgumentException("Unsupported arrange: " + document);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) DeleteResult(com.mongodb.client.result.DeleteResult) UpdateResult(com.mongodb.client.result.UpdateResult) BsonValue(org.bson.BsonValue)

Example 2 with DeleteResult

use of com.mongodb.client.result.DeleteResult in project mongo-java-driver by mongodb.

the class GridFSBucketImpl method delete.

@Override
public void delete(final BsonValue id) {
    DeleteResult result = filesCollection.deleteOne(new BsonDocument("_id", id));
    chunksCollection.deleteMany(new BsonDocument("files_id", id));
    if (result.wasAcknowledged() && result.getDeletedCount() == 0) {
        throw new MongoGridFSException(format("No file found with the id: %s", id));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) MongoGridFSException(com.mongodb.MongoGridFSException) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 3 with DeleteResult

use of com.mongodb.client.result.DeleteResult in project camel by apache.

the class MongoDbIdempotentRepository method remove.

@ManagedOperation(description = "Remove the key from the store")
@Override
public boolean remove(E key) {
    Document document = new Document("_id", key);
    DeleteResult res = collection.deleteOne(document);
    return res.getDeletedCount() > 0;
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DeleteResult(com.mongodb.client.result.DeleteResult) ManagedOperation(org.apache.camel.api.management.ManagedOperation)

Example 4 with DeleteResult

use of com.mongodb.client.result.DeleteResult in project camel by apache.

the class MongoDbOperationsTest method testRemove.

@Test
public void testRemove() throws Exception {
    // Prepare test
    assertEquals(0, testCollection.count());
    for (int i = 1; i <= 100; i++) {
        String body = null;
        Formatter f = new Formatter();
        if (i % 2 == 0) {
            body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString();
        } else {
            body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\", \"extraField\": true}", i).toString();
        }
        f.close();
        template.requestBody("direct:insert", body);
    }
    assertEquals(100L, testCollection.count());
    // Testing the update logic
    BasicDBObject extraField = new BasicDBObject("extraField", true);
    assertEquals("Number of records with 'extraField' flag on must equal 50", 50L, testCollection.count(extraField));
    Exchange resultExchange = template.request("direct:remove", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(extraField);
        }
    });
    Object result = resultExchange.getOut().getBody();
    assertTrue(result instanceof DeleteResult);
    assertEquals("Number of records deleted header should equal 50", 50L, resultExchange.getOut().getHeader(MongoDbConstants.RECORDS_AFFECTED));
    assertEquals("Number of records with 'extraField' flag on must be 0 after remove", 0, testCollection.count(extraField));
}
Also used : Exchange(org.apache.camel.Exchange) BasicDBObject(com.mongodb.BasicDBObject) Processor(org.apache.camel.Processor) Formatter(java.util.Formatter) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) DeleteResult(com.mongodb.client.result.DeleteResult) Test(org.junit.Test)

Example 5 with DeleteResult

use of com.mongodb.client.result.DeleteResult in project camel by apache.

the class MongoDbOperationsTest method testRemove.

@Test
public void testRemove() throws Exception {
    // Prepare test
    assertEquals(0, testCollection.count());
    for (int i = 1; i <= 100; i++) {
        String body = null;
        try (Formatter f = new Formatter()) {
            if (i % 2 == 0) {
                body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString();
            } else {
                body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\", \"extraField\": true}", i).toString();
            }
            f.close();
        }
        template.requestBody("direct:insert", body);
    }
    assertEquals(100L, testCollection.count());
    // Testing the update logic
    Document extraField = new Document("extraField", true);
    assertEquals("Number of records with 'extraField' flag on must equal 50", 50L, testCollection.count(extraField));
    Exchange resultExchange = template.request("direct:remove", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(extraField);
        }
    });
    Object result = resultExchange.getOut().getBody();
    assertTrue(result instanceof DeleteResult);
    assertEquals("Number of records deleted header should equal 50", 50L, resultExchange.getOut().getHeader(MongoDbConstants.RECORDS_AFFECTED));
    assertEquals("Number of records with 'extraField' flag on must be 0 after remove", 0, testCollection.count(extraField));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Formatter(java.util.Formatter) Document(org.bson.Document) DeleteResult(com.mongodb.client.result.DeleteResult) Test(org.junit.Test)

Aggregations

DeleteResult (com.mongodb.client.result.DeleteResult)46 Document (org.bson.Document)20 UpdateResult (com.mongodb.client.result.UpdateResult)10 BsonDocument (org.bson.BsonDocument)9 Test (org.junit.jupiter.api.Test)9 BasicDBObject (com.mongodb.BasicDBObject)7 WriteConcern (com.mongodb.WriteConcern)6 Bson (org.bson.conversions.Bson)6 MongoException (com.mongodb.MongoException)5 ArrayList (java.util.ArrayList)5 DataAccessException (org.springframework.dao.DataAccessException)5 MongoGridFSException (com.mongodb.MongoGridFSException)4 MongoClient (com.mongodb.client.MongoClient)4 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)4 ReadPreference (com.mongodb.ReadPreference)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 InsertOneResult (com.mongodb.client.result.InsertOneResult)3 MongoClient (com.mongodb.reactivestreams.client.MongoClient)3 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)3 ObjectId (org.bson.types.ObjectId)3