Как и для match/unwind
AddFieldOperation отсутствует в spring data mongo
Но вы можете написать свой собственный класс, а также класс custom Aggregation
для добавления метода вызова к addFieldOpration
, как показано ниже.
public class AddFieldOperation implements AggregationOperation {
private final Document document;
/**
* Creates a new {@link MatchOperation} for the given {@link CriteriaDefinition}.
*
* @param criteriaDefinition must not be {@literal null}.
*/
public AddFieldOperation(final Document document) {
Assert.notNull(document, "Criteria must not be null!");
this.document = document;
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDocument(org.
* springframework.data.mongodb.core.aggregation.AggregationOperationContext)
*/
@Override
public Document toDocument(final AggregationOperationContext context) {
return new Document("$addFields", this.document);
}
}
Теперь создайте класс CustomAggregation.
public class CustomAggregation extends Aggregation {
public static AddFieldOperation addField(final Document document) {
return new AddFieldOperation(document);
}
}
Все готово, вам нужно вызвать метод Addfield
и передать все запросы в примере объекта Document: -
AddFieldOperation addField =
CustomAggregation.addField(new Document().append("fieldName", fieldValue));
Примечание
Document
класс от import package org.bson.Document
;
Который является представлением документа в виде {@code Map}.
Все операции агрегации, реализованные в spring data mongo
, в конечном итоге преобразуются в объект Document, и это выполняется в оболочке. Так что, если некоторые из конвейеров агрегации еще не реализованы в spirng data
, в этом случае мы можем написать свой собственный и передать запрос, написанный в оболочке mongo, мы можем просто передать его в объект Document.