我希望补充我的解决方案,这在某些方面与之前发布的解决方案有所重复。
Mongo驱动程序v3.x
对于Mongo驱动程序v3.x,我采用了以下解决方案:
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.util.JSON;
import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
public class JsonOperation implements AggregationOperation {
private List<Document> documents;
public JsonOperation(String json) {
Object root = JSON.parse(json);
documents = root instanceof BasicDBObject
? Collections.singletonList(new Document(((BasicDBObject) root).toMap()))
: ((BasicDBList) root).stream().map(item -> new Document((Map<String, Object>) ((BasicDBObject) item).toMap())).collect(Collectors.toList());
}
@Override
public Document toDocument(AggregationOperationContext context) {
return null;
}
@Override
public List<Document> toPipelineStages(AggregationOperationContext context) {
return documents;
}
}
假设一些资源中提供了聚合步骤,例如aggregations.json
:
[
{
$match: {
"userId": "..."
}
},
{
$lookup: {
let: {
...
},
from: "another_collection",
pipeline: [
...
],
as: "things"
}
},
{
$sort: {
"date": 1
}
}
]
可以按以下方式使用上述类:
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
Collection<ResultDao> results = mongoTemplate.aggregate(newAggregation(new JsonOperation(resourceToString("aggregations.json", StandardCharsets.UTF_8))), "some_collection", ResultDao.class).getMappedResults();
Mongo driver v4.x
在Mongo v4版本中,JSON
类被移除,因此我已将该类重写如下:
import java.util.Collections;
import java.util.List;
import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
public class JsonOperation implements AggregationOperation {
private List<Document> documents;
private static final String DUMMY_KEY = "dummy";
public JsonOperation(String json) {
documents = parseJson(json);
}
static final List<Document> parseJson(String json) {
return (json.startsWith("["))
? Document.parse("{\"" + DUMMY_KEY + "\": " + json + "}").getList(DUMMY_KEY, Document.class)
: Collections.singletonList(Document.parse(json));
}
@Override
public Document toDocument(AggregationOperationContext context) {
return null;
}
@Override
public List<Document> toPipelineStages(AggregationOperationContext context) {
return documents;
}
@Override
public String getOperator() {
return documents.iterator().next().keySet().iterator().next();
}
}
但是由于字符串操作的缘故,实现现在有点丑陋。如果有更好的想法可以更优雅地解析对象数组,请编辑此帖或留言评论。理想情况下,Mongo核心应该有一些方法可以解析JSON对象或列表(返回BasicDBObject
/BasicDBList
或Document
/List<Document>
)。
还要注意,在toPipelineStages()
方法中我省略了将Document
实例转换成管道阶段的步骤,因为在我的情况下这并不是必要的:
@Override
public List<Document> toPipelineStages(AggregationOperationContext context) {
return documents.stream().map(document -> context.getMappedObject(document)).collect(Collectors.toList());
}
@Override public ExposedFields getFields() { return ExposedFields.synthetic(Fields.fields(this.exposedField)); }
- Mohit Sharma