这是我的mongoDB聚合查询,可在mongoDB shell中完美运行:
const storage = this._storage.getStoredData();
我试图在Java中获得相同的东西Springboot。令我震惊的是获得“参与”部分,即获得这一点:
db.objectData.aggregate([{$project:{newFieldList:{$arrayToObject: { "$map" : { "input" : "$fieldList", "as" : "fieldList", "in" : { k: "$$fieldList.fieldVal", v: "$$fieldList.value" } } }}}}]).pretty()
这是我现在拥有的:
{ k: "$$fieldList.fieldVal", v: "$$fieldList.value" }
上面的代码打印:
Map map = VariableOperators.Map.itemsOf("fieldList").as("fieldList").andApply(ArrayOperators.ArrayElemAt.arrayOf("fieldList.fieldVal"));
ArrayToObject arrayToObject = ArrayOperators.ArrayToObject.arrayToObject(map);
ProjectionOperationBuilder projectBuilder = Aggregation.project().and(arrayToObject);
ProjectionOperation projectStage = projectBuilder.as("newFieldList");
Aggregation agg = Aggregation.newAggregation(projectStage);
System.out.println("query = " + agg.toString());
您能帮我找到要在springboot中用'andApply'编写的聚合表达式吗?
query = { "aggregate" : "__collection__", "pipeline" : [{ "$project" : { "newFieldList" : { "$arrayToObject" : { "$map" : { "input" : "$fieldList", "as" : "fieldList", "in" : { "$arrayElemAt" : ["$$fieldList.fieldVal"] } } } } } }] }