/** * @param query pass the given query though a {@link QueryMapper} to apply type conversion. * @return never {@literal null}. */ protected Document getMappedQuery(Document query) { return queryMapper.getMappedObject(query, Optional.empty()); }
@Override protected Entry<String, Object> getMappedObjectForField(Field field, Object rawValue) { if (isDocument(rawValue)) { Object val = field.isMap() ? new LinkedHashMap<>((Document) rawValue) : rawValue; // unwrap to preserve field type return createMapEntry(field, convertSimpleOrDocument(val, field.getPropertyEntity())); } if (isQuery(rawValue)) { return createMapEntry(field, super.getMappedObject(((Query) rawValue).getQueryObject(), field.getPropertyEntity())); } if (isUpdateModifier(rawValue)) { return getMappedUpdateModifier(field, rawValue); } return super.getMappedObjectForField(field, rawValue); }
public Document getMappedObject(Bson query, Optional<? extends MongoPersistentEntity<?>> entity) { return getMappedObject(query, entity.orElse(null)); }
private List<Document> mapAggregationPipeline(List<Document> pipeline) { return pipeline.stream().map(val -> queryMapper.getMappedObject(val, Optional.empty())) .collect(Collectors.toList()); } }
@Override public Document getMappedObject(Document document) { return mapper.getMappedObject(document, mappingContext.getPersistentEntity(type)); }
/** * Maps fields to retrieve to the {@link MongoPersistentEntity}s properties. <br /> * Also converts and potentially adds missing property {@code $meta} representation. * * @param fieldsObject must not be {@literal null}. * @param entity can be {@litearl null}. * @return * @since 1.6 */ public Document getMappedFields(Document fieldsObject, @Nullable MongoPersistentEntity<?> entity) { Assert.notNull(fieldsObject, "FieldsObject must not be null!"); Document mappedFields = fieldsObject.isEmpty() ? new Document() : getMappedObject(fieldsObject, entity); mapMetaAttributes(mappedFields, entity, MetaMapping.FORCE); return mappedFields; }
public long count(Query query, @Nullable Class<?> entityClass, String collectionName) { Assert.notNull(query, "Query must not be null!"); Assert.hasText(collectionName, "Collection name must not be null or empty!"); CountOptions options = new CountOptions(); query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation); Document document = queryMapper.getMappedObject(query.getQueryObject(), Optional.ofNullable(entityClass).map(it -> mappingContext.getPersistentEntity(entityClass))); return doCount(collectionName, document, options); }
private Bson getMappedQuery(Bson query) { return bulkOperationContext.getQueryMapper().getMappedObject(query, bulkOperationContext.getEntity()); }
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); }
@Override public boolean exists(Query query, @Nullable Class<?> entityClass, String collectionName) { if (query == null) { throw new InvalidDataAccessApiUsageException("Query passed in to exist can't be null"); } Assert.notNull(collectionName, "CollectionName must not be null!"); Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), getPersistentEntity(entityClass)); return execute(collectionName, new ExistsCallback(mappedQuery, query.getCollation().map(Collation::toMongoCollation).orElse(null))); }
public Mono<Long> count(Query query, @Nullable Class<?> entityClass, String collectionName) { Assert.notNull(query, "Query must not be null!"); Assert.hasText(collectionName, "Collection name must not be null or empty!"); return createMono(collectionName, collection -> { Document filter = query == null ? null : queryMapper.getMappedObject(query.getQueryObject(), entityClass == null ? null : mappingContext.getPersistentEntity(entityClass)); CountOptions options = new CountOptions(); if (query != null) { query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Executing count: {} in collection: {}", serializeToJsonSafely(filter), collectionName); } return collection.count(filter, options); }); }
public Mono<Boolean> exists(Query query, @Nullable Class<?> entityClass, String collectionName) { if (query == null) { throw new InvalidDataAccessApiUsageException("Query passed in to exist can't be null"); } return createFlux(collectionName, collection -> { Document filter = queryMapper.getMappedObject(query.getQueryObject(), getPersistentEntity(entityClass)); FindPublisher<Document> findPublisher = collection.find(filter, Document.class) .projection(new Document("_id", 1)); if (LOGGER.isDebugEnabled()) { LOGGER.debug("exists: {} in collection: {}", serializeToJsonSafely(filter), collectionName); } findPublisher = query.getCollation().map(Collation::toMongoCollation).map(findPublisher::collation) .orElse(findPublisher); return findPublisher.limit(1); }).hasElements(); }
Document getMappedValidator(Validator validator, Class<?> domainType) { Document validationRules = validator.toDocument(); if (validationRules.containsKey("$jsonSchema")) { return schemaMapper.mapSchema(validationRules, domainType); } return queryMapper.getMappedObject(validationRules, mappingContext.getPersistentEntity(domainType)); }
private Document getMappedValidator(Validator validator, Class<?> domainType) { Document validationRules = validator.toDocument(); if (validationRules.containsKey("$jsonSchema")) { return schemaMapper.mapSchema(validationRules, domainType); } return queryMapper.getMappedObject(validationRules, mappingContext.getPersistentEntity(domainType)); }
/** * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified targetClass while * using sourceClass for mapping the query. * * @since 2.0 */ <S, T> Flux<T> doFind(String collectionName, Document query, Document fields, Class<S> sourceClass, Class<T> targetClass, FindPublisherPreparer preparer) { MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(sourceClass); Document mappedFields = getMappedFieldsObject(fields, entity, targetClass); Document mappedQuery = queryMapper.getMappedObject(query, entity); if (LOGGER.isDebugEnabled()) { LOGGER.debug("find using query: {} fields: {} for class: {} in collection: {}", serializeToJsonSafely(mappedQuery), mappedFields, sourceClass, collectionName); } return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, new ProjectingReadCallback<>(mongoConverter, sourceClass, targetClass, collectionName), collectionName); }
protected <T> Mono<T> doFindAndModify(String collectionName, Document query, Document fields, Document sort, Class<T> entityClass, Update update, FindAndModifyOptions options) { MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass); return Mono.defer(() -> { increaseVersionForUpdateIfNecessary(entity, update); Document mappedQuery = queryMapper.getMappedObject(query, entity); Document mappedUpdate = updateMapper.getMappedObject(update.getUpdateObject(), entity); if (LOGGER.isDebugEnabled()) { LOGGER.debug(String.format( "findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s " + "in collection: %s", serializeToJsonSafely(mappedQuery), fields, sort, entityClass, serializeToJsonSafely(mappedUpdate), collectionName)); } return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options), new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName), collectionName); }); }
public Mono<String> ensureIndex(final IndexDefinition indexDefinition) { return mongoOperations.execute(collectionName, collection -> { Document indexOptions = indexDefinition.getIndexOptions(); IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition); if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) { Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY)); MongoPersistentEntity<?> entity = type .map(val -> (MongoPersistentEntity) queryMapper.getMappingContext().getRequiredPersistentEntity(val)) .orElseGet(() -> lookupPersistentEntity(collectionName)); ops = ops.partialFilterExpression( queryMapper.getMappedObject(indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY, Document.class), entity)); } return collection.createIndex(indexDefinition.getIndexKeys(), ops); }).next(); }
public String ensureIndex(final IndexDefinition indexDefinition) { return execute(collection -> { Document indexOptions = indexDefinition.getIndexOptions(); IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition); if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) { Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY)); ops.partialFilterExpression(mapper.getMappedObject((Document) indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), lookupPersistentEntity(type, collectionName))); } return collection.createIndex(indexDefinition.getIndexKeys(), ops); }); }
@Override public CloseableIterator<T> doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { MongoPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityType); Document mappedFields = getMappedFieldsObject(query.getFieldsObject(), persistentEntity, returnType); Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), persistentEntity); FindIterable<Document> cursor = new QueryCursorPreparer(query, entityType) .prepare(collection.find(mappedQuery, Document.class).projection(mappedFields)); return new CloseableIterableCursorAdapter<>(cursor, exceptionTranslator, new ProjectingReadCallback<>(mongoConverter, entityType, returnType, collectionName)); } });