use of com.mongodb.client.MongoIterable in project camel by apache.
the class MongoDbOutputTypeTest method testFindAllDBCursor.
@Test
public void testFindAllDBCursor() {
// Test that the collection has 0 documents in it
assertEquals(0, testCollection.count());
pumpDataIntoTestCollection();
// Repeat ten times, obtain 10 batches of 100 results each time
int numToSkip = 0;
final int limit = 100;
for (int i = 0; i < 10; i++) {
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(MongoDbConstants.NUM_TO_SKIP, numToSkip);
headers.put(MongoDbConstants.LIMIT, 100);
Object result = template.requestBodyAndHeaders("direct:findAllDBCursor", (Object) null, headers);
assertTrue("Result is not of type DBCursor", result instanceof MongoIterable);
MongoIterable<BasicDBObject> resultCursor = (MongoIterable<BasicDBObject>) result;
// Ensure that all returned documents contain all fields
for (DBObject dbObject : resultCursor) {
assertNotNull("DBObject in returned list should contain all fields", dbObject.get("_id"));
assertNotNull("DBObject in returned list should contain all fields", dbObject.get("scientist"));
assertNotNull("DBObject in returned list should contain all fields", dbObject.get("fixedField"));
}
numToSkip = numToSkip + limit;
}
}
use of com.mongodb.client.MongoIterable in project camel by apache.
the class MongoDbOutputTypeTest method testFindAllDBCursor.
@Test
public void testFindAllDBCursor() {
// Test that the collection has 0 documents in it
assertEquals(0, testCollection.count());
pumpDataIntoTestCollection();
// Repeat ten times, obtain 10 batches of 100 results each time
int numToSkip = 0;
final int limit = 100;
for (int i = 0; i < 10; i++) {
Map<String, Object> headers = new HashMap<>();
headers.put(MongoDbConstants.NUM_TO_SKIP, numToSkip);
headers.put(MongoDbConstants.LIMIT, 100);
Object result = template.requestBodyAndHeaders("direct:findAllDBCursor", ObjectUtils.NULL, headers);
assertTrue("Result is not of type MongoIterable", result instanceof MongoIterable);
@SuppressWarnings("unchecked") MongoIterable<Document> resultCursor = (MongoIterable<Document>) result;
// Ensure that all returned documents contain all fields
for (Document document : resultCursor) {
assertNotNull("Document in returned list should contain all fields", document.get(MONGO_ID));
assertNotNull("Document in returned list should contain all fields", document.get("scientist"));
assertNotNull("Document in returned list should contain all fields", document.get("fixedField"));
}
numToSkip = numToSkip + limit;
}
}
use of com.mongodb.client.MongoIterable in project spring-data-mongodb by spring-projects.
the class MongoTemplate method findDistinct.
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.MongoOperations#findDistinct(org.springframework.data.mongodb.core.query.Query, java.lang.String, java.lang.String, java.lang.Class, java.lang.Class)
*/
@Override
@SuppressWarnings("unchecked")
public <T> List<T> findDistinct(Query query, String field, String collectionName, Class<?> entityClass, Class<T> resultClass) {
Assert.notNull(query, "Query must not be null!");
Assert.notNull(field, "Field must not be null!");
Assert.notNull(collectionName, "CollectionName must not be null!");
Assert.notNull(entityClass, "EntityClass must not be null!");
Assert.notNull(resultClass, "ResultClass must not be null!");
MongoPersistentEntity<?> entity = entityClass != Object.class ? getPersistentEntity(entityClass) : null;
DistinctQueryContext distinctQueryContext = queryOperations.distinctQueryContext(query, field);
Document mappedQuery = distinctQueryContext.getMappedQuery(entity);
String mappedFieldName = distinctQueryContext.getMappedFieldName(entity);
Class<T> mongoDriverCompatibleType = distinctQueryContext.getDriverCompatibleClass(resultClass);
MongoIterable<?> result = execute(collectionName, (collection) -> {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Executing findDistinct using query %s for field: %s in collection: %s", serializeToJsonSafely(mappedQuery), field, collectionName));
}
QueryCursorPreparer preparer = new QueryCursorPreparer(query, entityClass);
if (preparer.hasReadPreference()) {
collection = collection.withReadPreference(preparer.getReadPreference());
}
DistinctIterable<T> iterable = collection.distinct(mappedFieldName, mappedQuery, mongoDriverCompatibleType);
distinctQueryContext.applyCollation(entityClass, iterable::collation);
return iterable;
});
if (resultClass == Object.class || mongoDriverCompatibleType != resultClass) {
MongoConverter converter = getConverter();
DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
result = result.map((source) -> converter.mapValueToTargetType(source, distinctQueryContext.getMostSpecificConversionTargetType(resultClass, entityClass), dbRefResolver));
}
try {
return (List<T>) result.into(new ArrayList<>());
} catch (RuntimeException e) {
throw potentiallyConvertRuntimeException(e, exceptionTranslator);
}
}
Aggregations