private Document getMappedFieldsObject(Document fields, MongoPersistentEntity<?> entity, Class<?> targetType) { Document projectedFields = propertyOperations.computeFieldsForProjection(projectionFactory, fields, entity.getType(), targetType); if (ObjectUtils.nullSafeEquals(fields, projectedFields)) { return queryMapper.getMappedFields(projectedFields, entity); } return queryMapper.getMappedFields(projectedFields, mappingContext.getRequiredPersistentEntity(targetType)); }
private Document getMappedFieldsObject(Document fields, MongoPersistentEntity<?> entity, Class<?> targetType) { Document projectedFields = propertyOperations.computeFieldsForProjection(projectionFactory, fields, entity.getType(), targetType); if (ObjectUtils.nullSafeEquals(fields, projectedFields)) { return queryMapper.getMappedFields(projectedFields, entity); } return queryMapper.getMappedFields(projectedFields, mappingContext.getRequiredPersistentEntity(targetType)); }
protected <S, T> Flux<T> doFind(String collectionName, Document query, Document fields, Class<S> entityClass, @Nullable FindPublisherPreparer preparer, DocumentCallback<T> objectCallback) { MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass); Document mappedFields = queryMapper.getMappedFields(fields, entity); Document mappedQuery = queryMapper.getMappedObject(query, entity); if (LOGGER.isDebugEnabled()) { LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s", serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName)); } return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback, collectionName); }
protected <S, T> List<T> doFind(String collectionName, Document query, Document fields, Class<S> entityClass, @Nullable CursorPreparer preparer, DocumentCallback<T> objectCallback) { MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass); Document mappedFields = queryMapper.getMappedFields(fields, entity); Document mappedQuery = queryMapper.getMappedObject(query, entity); if (LOGGER.isDebugEnabled()) { LOGGER.debug("find using query: {} fields: {} for class: {} in collection: {}", serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName); } return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback, collectionName); }
@SuppressWarnings("unchecked") public <T> Flux<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 = getPersistentEntity(entityClass); Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), entity); String mappedFieldName = queryMapper.getMappedFields(new Document(field, 1), entity).keySet().iterator().next(); Class<T> mongoDriverCompatibleType = mongoDatabaseFactory.getCodecFor(resultClass) // .map(Codec::getEncoderClass) // .orElse((Class<T>) BsonValue.class); Flux<?> result = execute(collectionName, collection -> { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Executing findDistinct using query {} for field: {} in collection: {}", serializeToJsonSafely(mappedQuery), field, collectionName); } DistinctPublisher<T> publisher = collection.distinct(mappedFieldName, mappedQuery, mongoDriverCompatibleType); return query.getCollation().map(Collation::toMongoCollation).map(publisher::collation).orElse(publisher); }); if (resultClass == Object.class || mongoDriverCompatibleType != resultClass) { Class<?> targetType = getMostSpecificConversionTargetType(resultClass, entityClass, field); MongoConverter converter = getConverter(); result = result.map(it -> converter.mapValueToTargetType(it, targetType, NO_OP_REF_RESOLVER)); } return (Flux<T>) result; }
@Override public <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType, String collectionName, Class<T> resultType) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(replacement, "Replacement must not be null!"); Assert.notNull(options, "Options must not be null! Use FindAndReplaceOptions#empty() instead."); Assert.notNull(entityType, "EntityType must not be null!"); Assert.notNull(collectionName, "CollectionName must not be null!"); Assert.notNull(resultType, "ResultType must not be null! Use Object.class instead."); Assert.isTrue(query.getLimit() <= 1, "Query must not define a limit other than 1 ore none!"); Assert.isTrue(query.getSkip() <= 0, "Query must not define skip."); MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType); Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), entity); Document mappedFields = queryMapper.getMappedFields(query.getFieldsObject(), entity); Document mappedSort = queryMapper.getMappedSort(query.getSortObject(), entity); Document mappedReplacement = operations.forEntity(replacement).toMappedDocument(this.mongoConverter).getDocument(); return doFindAndReplace(collectionName, mappedQuery, mappedFields, mappedSort, query.getCollation().map(Collation::toMongoCollation).orElse(null), entityType, mappedReplacement, options, resultType); }
@Override public <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType, String collectionName, Class<T> resultType) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(replacement, "Replacement must not be null!"); Assert.notNull(options, "Options must not be null! Use FindAndReplaceOptions#empty() instead."); Assert.notNull(entityType, "Entity class must not be null!"); Assert.notNull(collectionName, "CollectionName must not be null!"); Assert.notNull(resultType, "ResultType must not be null! Use Object.class instead."); Assert.isTrue(query.getLimit() <= 1, "Query must not define a limit other than 1 ore none!"); Assert.isTrue(query.getSkip() <= 0, "Query must not define skip."); MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType); Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), entity); Document mappedFields = queryMapper.getMappedFields(query.getFieldsObject(), entity); Document mappedSort = queryMapper.getMappedSort(query.getSortObject(), entity); Document mappedReplacement = operations.forEntity(replacement).toMappedDocument(this.mongoConverter).getDocument(); return doFindAndReplace(collectionName, mappedQuery, mappedFields, mappedSort, query.getCollation().map(Collation::toMongoCollation).orElse(null), entityType, mappedReplacement, options, resultType); }
String mappedFieldName = queryMapper.getMappedFields(new Document(field, 1), entity).keySet().iterator().next();
private Document getMappedFieldsObject(Document fields, MongoPersistentEntity<?> entity, Class<?> targetType) { Document projectedFields = propertyOperations.computeFieldsForProjection(projectionFactory, fields, entity.getType(), targetType); if (ObjectUtils.nullSafeEquals(fields, projectedFields)) { return queryMapper.getMappedFields(projectedFields, entity); } return queryMapper.getMappedFields(projectedFields, mappingContext.getRequiredPersistentEntity(targetType)); }
private Document getMappedFieldsObject(Document fields, MongoPersistentEntity<?> entity, Class<?> targetType) { Document projectedFields = propertyOperations.computeFieldsForProjection(projectionFactory, fields, entity.getType(), targetType); if (ObjectUtils.nullSafeEquals(fields, projectedFields)) { return queryMapper.getMappedFields(projectedFields, entity); } return queryMapper.getMappedFields(projectedFields, mappingContext.getRequiredPersistentEntity(targetType)); }
protected <S, T> Flux<T> doFind(String collectionName, Document query, Document fields, Class<S> entityClass, @Nullable FindPublisherPreparer preparer, DocumentCallback<T> objectCallback) { MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass); Document mappedFields = queryMapper.getMappedFields(fields, entity); Document mappedQuery = queryMapper.getMappedObject(query, entity); if (LOGGER.isDebugEnabled()) { LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s", serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName)); } return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback, collectionName); }
protected <S, T> List<T> doFind(String collectionName, Document query, Document fields, Class<S> entityClass, @Nullable CursorPreparer preparer, DocumentCallback<T> objectCallback) { MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass); Document mappedFields = queryMapper.getMappedFields(fields, entity); Document mappedQuery = queryMapper.getMappedObject(query, entity); if (LOGGER.isDebugEnabled()) { LOGGER.debug("find using query: {} fields: {} for class: {} in collection: {}", serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName); } return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback, collectionName); }
@SuppressWarnings("unchecked") public <T> Flux<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 = getPersistentEntity(entityClass); Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), entity); String mappedFieldName = queryMapper.getMappedFields(new Document(field, 1), entity).keySet().iterator().next(); Class<T> mongoDriverCompatibleType = mongoDatabaseFactory.getCodecFor(resultClass) // .map(Codec::getEncoderClass) // .orElse((Class<T>) BsonValue.class); Flux<?> result = execute(collectionName, collection -> { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Executing findDistinct using query {} for field: {} in collection: {}", serializeToJsonSafely(mappedQuery), field, collectionName); } DistinctPublisher<T> publisher = collection.distinct(mappedFieldName, mappedQuery, mongoDriverCompatibleType); return query.getCollation().map(Collation::toMongoCollation).map(publisher::collation).orElse(publisher); }); if (resultClass == Object.class || mongoDriverCompatibleType != resultClass) { Class<?> targetType = getMostSpecificConversionTargetType(resultClass, entityClass, field); MongoConverter converter = getConverter(); result = result.map(it -> converter.mapValueToTargetType(it, targetType, NO_OP_REF_RESOLVER)); } return (Flux<T>) result; }
@Override public <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType, String collectionName, Class<T> resultType) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(replacement, "Replacement must not be null!"); Assert.notNull(options, "Options must not be null! Use FindAndReplaceOptions#empty() instead."); Assert.notNull(entityType, "EntityType must not be null!"); Assert.notNull(collectionName, "CollectionName must not be null!"); Assert.notNull(resultType, "ResultType must not be null! Use Object.class instead."); Assert.isTrue(query.getLimit() <= 1, "Query must not define a limit other than 1 ore none!"); Assert.isTrue(query.getSkip() <= 0, "Query must not define skip."); MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType); Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), entity); Document mappedFields = queryMapper.getMappedFields(query.getFieldsObject(), entity); Document mappedSort = queryMapper.getMappedSort(query.getSortObject(), entity); Document mappedReplacement = operations.forEntity(replacement).toMappedDocument(this.mongoConverter).getDocument(); return doFindAndReplace(collectionName, mappedQuery, mappedFields, mappedSort, query.getCollation().map(Collation::toMongoCollation).orElse(null), entityType, mappedReplacement, options, resultType); }
@Override public <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType, String collectionName, Class<T> resultType) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(replacement, "Replacement must not be null!"); Assert.notNull(options, "Options must not be null! Use FindAndReplaceOptions#empty() instead."); Assert.notNull(entityType, "Entity class must not be null!"); Assert.notNull(collectionName, "CollectionName must not be null!"); Assert.notNull(resultType, "ResultType must not be null! Use Object.class instead."); Assert.isTrue(query.getLimit() <= 1, "Query must not define a limit other than 1 ore none!"); Assert.isTrue(query.getSkip() <= 0, "Query must not define skip."); MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType); Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), entity); Document mappedFields = queryMapper.getMappedFields(query.getFieldsObject(), entity); Document mappedSort = queryMapper.getMappedSort(query.getSortObject(), entity); Document mappedReplacement = operations.forEntity(replacement).toMappedDocument(this.mongoConverter).getDocument(); return doFindAndReplace(collectionName, mappedQuery, mappedFields, mappedSort, query.getCollation().map(Collation::toMongoCollation).orElse(null), entityType, mappedReplacement, options, resultType); }
String mappedFieldName = queryMapper.getMappedFields(new Document(field, 1), entity).keySet().iterator().next();