Search in sources :

Example 1 with CountOptions

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

the class FindAndDeleteAcceptanceTest method shouldRemoveOnlyOneDocumentWhenMultipleDocumentsMatchSearch.

@Test
public void shouldRemoveOnlyOneDocumentWhenMultipleDocumentsMatchSearch() {
    // given
    Document documentInserted = new Document(KEY, VALUE_TO_CARE_ABOUT);
    collection.insertOne(documentInserted);
    collection.insertOne(new Document(KEY, VALUE_TO_CARE_ABOUT));
    collection.insertOne(new Document(KEY, VALUE_TO_CARE_ABOUT));
    assertThat(collection.count(), is(3L));
    // when
    Document filter = new Document(KEY, VALUE_TO_CARE_ABOUT);
    assertThat(collection.count(filter, new CountOptions()), is(3L));
    Document documentRetrieved = collection.findOneAndDelete(filter);
    // then
    assertThat("Document should have been deleted from the collection", collection.count(), is(2L));
    assertThat("Document retrieved from removeAndGet should match the document that was inserted", documentRetrieved, equalTo(documentInserted));
}
Also used : CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) Test(org.junit.Test)

Example 2 with CountOptions

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

the class UpdateAcceptanceTest method shouldUpdateAllDocumentsIfUpdatingAllWithUpsertAndMultipleDocumentsFoundInCollection.

@Test
public void shouldUpdateAllDocumentsIfUpdatingAllWithUpsertAndMultipleDocumentsFoundInCollection() {
    // given
    Document firstDocument = new Document("_id", 1).append("x", 3);
    collection.insertOne(firstDocument);
    Document secondDocument = new Document("_id", 2).append("x", 3);
    collection.insertOne(secondDocument);
    // when
    Document filter = new Document("x", 3);
    collection.updateMany(filter, new Document("$set", new Document("x", 5)), new UpdateOptions().upsert(true));
    // then
    assertThat(collection.count(new Document("x", 5), new CountOptions()), is(2L));
}
Also used : CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) Test(org.junit.Test)

Example 3 with CountOptions

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

the class UpdateAcceptanceTest method shouldUpdateOneDocumentIfUpdatingOneWithUpsertAndMultipleDocumentsFoundInCollection.

@Test
public void shouldUpdateOneDocumentIfUpdatingOneWithUpsertAndMultipleDocumentsFoundInCollection() {
    // given
    Document firstDocument = new Document("_id", 1).append("x", 3);
    collection.insertOne(firstDocument);
    Document secondDocument = new Document("_id", 2).append("x", 3);
    collection.insertOne(secondDocument);
    // when
    Document filter = new Document("x", 3);
    collection.updateOne(filter, new Document("$set", new Document("x", 5)), new UpdateOptions().upsert(true));
    // then
    assertThat(collection.count(new Document("x", 5), new CountOptions()), is(1L));
}
Also used : CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) Test(org.junit.Test)

Example 4 with CountOptions

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

the class MongoCollectionImplTest method testCountDocuments.

@Test
public void testCountDocuments() {
    CountOptions options = new CountOptions().collation(Collation.builder().locale("de").build());
    assertAll("countDocuments", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments((Bson) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments(filter, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments((ClientSession) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments(clientSession, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments(clientSession, filter, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments(null, filter)), () -> assertThrows(IllegalArgumentException.class, () -> collection.countDocuments(null, filter, options))), () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(null, new BsonDocument(), new CountOptions());
        assertPublisherIsTheSameAs(expected, collection.countDocuments(), "Default");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(null, filter, new CountOptions());
        assertPublisherIsTheSameAs(expected, collection.countDocuments(filter), "With filter");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(null, filter, options);
        assertPublisherIsTheSameAs(expected, collection.countDocuments(filter, options), "With filter & options");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(clientSession, new BsonDocument(), new CountOptions());
        assertPublisherIsTheSameAs(expected, collection.countDocuments(clientSession), "With client session");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(clientSession, filter, new CountOptions());
        assertPublisherIsTheSameAs(expected, collection.countDocuments(clientSession, filter), "With client session & filter");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.countDocuments(clientSession, filter, options);
        assertPublisherIsTheSameAs(expected, collection.countDocuments(clientSession, filter, options), "With client session, filter & options");
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.reactivestreams.client.ClientSession) CountOptions(com.mongodb.client.model.CountOptions) EstimatedDocumentCountOptions(com.mongodb.client.model.EstimatedDocumentCountOptions) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 5 with CountOptions

use of com.mongodb.client.model.CountOptions in project gora by apache.

the class MongoStore method execute.

/**
 * Execute the query and return the result.
 */
@Override
public Result<K, T> execute(final Query<K, T> query) throws GoraException {
    try {
        String[] fields = getFieldsToQuery(query.getFields());
        // Build the actual MongoDB query
        Bson q = MongoDBQuery.toDBQuery(query);
        Bson p = MongoDBQuery.toProjection(fields, mapping);
        if (query.getFilter() != null) {
            Optional<Bson> filter = filterUtil.setFilter(query.getFilter(), this);
            if (!filter.isPresent()) {
                // don't need local filter
                query.setLocalFilterEnabled(false);
            } else {
                q = and(q, filter.get());
            }
        }
        // Execute the query on the collection
        FindIterable<Document> iterable = mongoClientColl.find(q).projection(p);
        CountOptions countOptions = new CountOptions();
        if (query.getLimit() > 0) {
            iterable.limit((int) query.getLimit());
            countOptions.limit((int) query.getLimit());
        }
        iterable.batchSize(100);
        iterable.noCursorTimeout(true);
        // Build the result
        long size = mongoClientColl.countDocuments(q, countOptions);
        return new MongoDBResult<>(this, query, iterable.cursor(), size);
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) MongoDBResult(org.apache.gora.mongodb.query.MongoDBResult) CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Bson(org.bson.conversions.Bson)

Aggregations

CountOptions (com.mongodb.client.model.CountOptions)16 Document (org.bson.Document)8 Test (org.junit.jupiter.api.Test)7 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)6 Test (org.junit.Test)5 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)4 Query (org.springframework.data.mongodb.core.query.Query)4 BsonDocument (org.bson.BsonDocument)3 Bson (org.bson.conversions.Bson)3 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 EstimatedDocumentCountOptions (com.mongodb.client.model.EstimatedDocumentCountOptions)1 ClientSession (com.mongodb.reactivestreams.client.ClientSession)1 StopWatch (core.framework.util.StopWatch)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 MongoDBResult (org.apache.gora.mongodb.query.MongoDBResult)1 GoraException (org.apache.gora.util.GoraException)1