Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用MongoTemplate将MongoShell脚本转换为Java?_Java_Mongodb_Spring Boot_Mongotemplate_Mongorepository - Fatal编程技术网

如何使用MongoTemplate将MongoShell脚本转换为Java?

如何使用MongoTemplate将MongoShell脚本转换为Java?,java,mongodb,spring-boot,mongotemplate,mongorepository,Java,Mongodb,Spring Boot,Mongotemplate,Mongorepository,我的Mongo Shell脚本: db.getCollection('order').aggregate([ {$match:{clientId:“test@gmail.com" } }, {$lookup:{from:'ordereddevice',localField:'id',foreignField:'order',as:'orderedDevices'}} ]) 我使用的是这样的: MongoClientURI连接字符串=新的MongoClientURI(“mongodb://loc

我的Mongo Shell脚本:

db.getCollection('order').aggregate([
{$match:{clientId:“test@gmail.com" } },
{$lookup:{from:'ordereddevice',localField:'id',foreignField:'order',as:'orderedDevices'}}
])
我使用的是这样的:

MongoClientURI连接字符串=新的MongoClientURI(“mongodb://localhost:27017");
MongoClient MongoClient=新的MongoClient(connectionString);
MongoDatabase=mongoClient.getDatabase(“db_名称”);
MongoCollectioncollection=database.getCollection(“订单”);
Listpipeline=Arrays.asList(新文档().append($match),新文档().append('clientId','test@gmail.com新文档().append($lookup),新文档().append(“from”,“ordereddevice”).append(“localField”,“id”).append(“foreignField”,“order”).append(“as”,“orderedDevices”));
块<文档>打印块=新块<文档>(){
@覆盖公共无效应用(最终文件){
System.out.println(document.get(“_id”);
}
};
集合。聚合(管道)。forEach(打印块);

但是它会重新连接MongoDB,所以我正在寻找一种使用MongoTemplate的方法来完成它

谢谢你的文档,我自己来完成吧!哈哈

@Autowired
MongoTemplate MongoTemplate;
@凌驾
公共列表FindOrderedeviceByOrderId(字符串clientid){
AggregationOperation lookup=Aggregation.lookup(“ordereddevice”、“_id”、“order”、“Devices”);
AggregationOperation match=Aggregation.match(Criteria.where(“clientId”).is(clientId));
Aggregation agg=Aggregation.newAggregation(匹配、查找);
AggregationResults=mongoTemplate.aggregate(agg,“order”,OrderedDeviceByOrderId.class);
List orderedDeviceByOrderId=results.getMappedResults();
orderedDeviceByOrderId.forEach->System.out.println;
返回orderedDeviceByOrderId;
}

以下是参考文档和使用示例。请张贴您有任何具体问题。您只是想使用
mongoTemplate
,还是存在特定问题?