【发布时间】:2020-05-07 11:13:29
【问题描述】:
我的 Mongo Shell 脚本:
db.getCollection('order').aggregate([
{ $match: { clientId: "test@gmail.com" } },
{ $lookup: { from: 'ordereddevice', localField: 'id', foreignField: 'order', as: 'orderedDevices' } }
])
我用的是这样的:
MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
MongoClient mongoClient = new MongoClient(connectionString);
MongoDatabase database = mongoClient.getDatabase("db_name");
MongoCollection < Document > collection = database.getCollection("order");
List < Document > pipeline = Arrays.asList(new Document().append("$match", new Document().append("clientId", "test@gmail.com")), new Document().append("$lookup", new Document().append("from", "ordereddevice").append("localField", "id").append("foreignField", "order").append("as", "orderedDevices")));
Block < Document > printBlock = new Block < Document > () {
@Override public void apply(final Document document) {
System.out.println(document.get("_id"));
}
};
collection.aggregate(pipeline).forEach(printBlock);
但它会重新连接MongoDB,所以我正在寻找一种使用MongoTemplate的方法来做到这一点
【问题讨论】:
-
这里是参考文档,其中包含使用 mongoTemplate 的示例。请发布您的任何具体问题。您只是想使用
mongoTemplate还是有具体问题吗?
标签: java mongodb spring-boot mongotemplate mongorepository