如何在 Java 中实现 addFields mongoDB 查询
How can I implement addFields mongoDB query in Java
我在 Whosebug 上找到了几个与聚合中的 addFields 相关的示例。
但是在 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" }}
])
我无法在 Java 中正确实施:-- 没有获得正确的程序
LookupOperation lookupOperation =LookupOperation.newLookup().from("simple").localField("execId.ref.uuid").foreignField("uuid").as("simple");
Aggregation myDocAggr = newAggregation(match(Criteria.where("metaId.ref.uuid").is(someUUID)), group("uuid").max("version").as("version"),
lookupOperation,
Aggregates.unwind(""),
Aggregates.addFields(fields));
Document document =new Document();
AggregationResults<String> myDocAggrResults = mongoTemplate.aggregate(myDocAggr , myDocument, myDocument.class);
List<String> mydocumentList = myDocAggrResults .getMappedResults();
无法使用 unwind 和 addFields,这是示例 java 代码,但不行。
请帮我。提前致谢
您将 java 驱动程序 Aggregates
方法与 Spring Aggregation
方法混合使用。
另外 $addFields
在 spring mongo 中仍然是 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();
虽然spring不支持$addFields mongo你可以自己实现:
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");
...
希望对大家有所帮助。
从 'spring-data-mongodb' 的 3.0 版开始,实际上可以非常轻松地创建 AddField 聚合。您可以使用 AddFieldsOperationBuilder.
在下面的示例中,我为名为 _id
的 objectId 字段添加了一个字符串 id 字段。
字符串字段 idString
可以在查找聚合中使用。
示例:
AddFieldsOperation addFieldsOperation = Aggregation.addFields().addFieldWithValue("idString", ConvertOperators.ToString.toString("$_id")).build();
我在 Whosebug 上找到了几个与聚合中的 addFields 相关的示例。 但是在 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" }}
])
我无法在 Java 中正确实施:-- 没有获得正确的程序
LookupOperation lookupOperation =LookupOperation.newLookup().from("simple").localField("execId.ref.uuid").foreignField("uuid").as("simple");
Aggregation myDocAggr = newAggregation(match(Criteria.where("metaId.ref.uuid").is(someUUID)), group("uuid").max("version").as("version"),
lookupOperation,
Aggregates.unwind(""),
Aggregates.addFields(fields));
Document document =new Document();
AggregationResults<String> myDocAggrResults = mongoTemplate.aggregate(myDocAggr , myDocument, myDocument.class);
List<String> mydocumentList = myDocAggrResults .getMappedResults();
无法使用 unwind 和 addFields,这是示例 java 代码,但不行。 请帮我。提前致谢
您将 java 驱动程序 Aggregates
方法与 Spring Aggregation
方法混合使用。
另外 $addFields
在 spring mongo 中仍然是 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();
虽然spring不支持$addFields mongo你可以自己实现:
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");
...
希望对大家有所帮助。
从 'spring-data-mongodb' 的 3.0 版开始,实际上可以非常轻松地创建 AddField 聚合。您可以使用 AddFieldsOperationBuilder.
在下面的示例中,我为名为 _id
的 objectId 字段添加了一个字符串 id 字段。
字符串字段 idString
可以在查找聚合中使用。
示例:
AddFieldsOperation addFieldsOperation = Aggregation.addFields().addFieldWithValue("idString", ConvertOperators.ToString.toString("$_id")).build();