如何在Java中实现addFields mongoDB查询

时间:2018-11-12 11:52:51

标签: java mongodb mongodb-query aggregation-framework spring-data-mongodb

我在StackOverFlow上发现了几个与Aggregation中的addFields相关的示例。 但是没有人用Java实现。

where (user_id, customer_id) in ( (u1, c1), (u2, c2), . . . )

我不能正确地用Java实现:-没有正确的程序

db.getCollection('myDocument').aggregate([
    {$match : {"metaId.ref.uuid" : "d6112808-1ce1-4545-bd52-cf55bc4ed25e"}},
    {$lookup: {from: "simple", localField: "someId.ref.uuid", foreignField: "uuid", 
    as: "simple"}},
    {"$unwind": "$simple"},
    {"$addFields": { "metaId.ref.name" : "$simple.name" }}
])

不能使用unwind和addFields,这是示例Java代码,但是不行。 请帮我。预先感谢

3 个答案:

答案 0 :(得分:5)

您正在将Java驱动程序Aggregates方法与Spring Aggregation方法混合使用。

在春季蒙哥,$addFields仍然是not supported

您必须使用以下汇总。

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
Aggregation myDocAggr = newAggregation(
       match(Criteria.where("metaId.ref.uuid").is(someUUID)), 
       group("uuid").max("version").as("version"),
       lookup("simple","execId.ref.uuid","uuid","simple"),
       unwind("simple"),
       new AggregationOperation(){ 
         @Override 
         public Document toDocument(AggregationOperationContext aoc) {
            return new Document("$addFields",new Document("metaId.ref.name","$simple.name"));
         }
      }
)
List<Document> mydocumentList=mongoTemplate.aggregate(myDocAggr,"myDocument",Document.class).getMappedResults();

答案 1 :(得分:3)

尽管Spring mongo不支持$ addFields,但是您可以自己实现:

import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.*;
import java.util.LinkedHashMap;
import java.util.Map;

public class AddFieldsOperation implements FieldsExposingAggregationOperation {
    private Map<String, Object> fields = new LinkedHashMap<>();

    public AddFieldsOperation(String field, AggregationExpression expression) {
        addField(field, expression);
    }

    public AddFieldsOperation addField(String field, AggregationExpression expression) {
        this.fields.put(field, expression.toDocument(Aggregation.DEFAULT_CONTEXT));
        return this;
    }

    @Override
    public Document toDocument(AggregationOperationContext context) {
        Document doc = new Document();
        fields.forEach(doc::append);
        return new Document("$addFields", doc);
    }

    @Override
    public boolean inheritsFields() {
        return true;
    }

    @Override
    public ExposedFields getFields() {
        final String[] fieldsArray = fields.keySet().toArray(new String[0]);
        return ExposedFields.synthetic(Fields.fields(fieldsArray));
    }

并像这样使用它:

...
ArithmeticOperators.Add value1 = ArithmeticOperators.Add.valueOf(0);
ArithmeticOperators.Add value2 = ArithmeticOperators.Add.valueOf(0);
AddFieldsOperation addFields
            = new AddFieldsOperation("value1", value1)
            .addField("value2", value2);
pipeline.add(addFields);
...
Add value1PlusValue2 = Add.valueOf("$value1").add("$value2");
...

希望它将对某人有所帮助。

答案 2 :(得分:2)

从'spring-data-mongodb'的3.0版开始,实际上很容易创建AddField聚集。您可以使用AddFieldsOperationBuilder

在下面的示例中,我为名为_id的objectId字段添加了一个字符串ID字段。 字符串字段idString可用于查找聚合。

示例:

    AddFieldsOperation addFieldsOperation = Aggregation.addFields().addFieldWithValue("idString", ConvertOperators.ToString.toString("$_id")).build();