Sheel:
私は集計中addFieldsに関連のStackOverflowでいくつかの例を発見しました。しかし、誰が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();
アンワインドとaddFieldsを使用することはできません、これはサンプルJavaコードですが、それは大丈夫ではありません。私を助けてください。前もって感謝します
user2683814:
あなたは、Javaのドライバ混合されているAggregates
スプリングと方法を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();