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));
}
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));
}
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));
}
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");
});
}
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);
}
}
Aggregations