如何使用 Mongo 模板将 Mongo Shell 脚本转换为 Java?
How to turn Mongo Shell script into Java using MongoTemplate?
我的MongoShell脚本:
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);
但是它会重新连接Mongo数据库,所以我正在寻找一种使用Mongo模板的方法来完成
感谢文档,自己完成吧!哈哈
@Autowired
MongoTemplate mongoTemplate;
@Override
public List<OrderedDeviceByOrderId> findOrderedDeviceByOrderId(String clientid) {
AggregationOperation lookup = Aggregation.lookup("ordereddevice","_id","order","Devices");
AggregationOperation match = Aggregation.match(Criteria.where("clientId").is(clientid));
Aggregation agg = Aggregation.newAggregation(match, lookup);
AggregationResults<OrderedDeviceByOrderId> results = mongoTemplate.aggregate(agg, "order", OrderedDeviceByOrderId.class);
List<OrderedDeviceByOrderId> orderedDeviceByOrderId = results.getMappedResults();
orderedDeviceByOrderId.forEach(s -> System.out.println(s));
return orderedDeviceByOrderId;
}
我的MongoShell脚本:
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);
但是它会重新连接Mongo数据库,所以我正在寻找一种使用Mongo模板的方法来完成
感谢文档,自己完成吧!哈哈
@Autowired
MongoTemplate mongoTemplate;
@Override
public List<OrderedDeviceByOrderId> findOrderedDeviceByOrderId(String clientid) {
AggregationOperation lookup = Aggregation.lookup("ordereddevice","_id","order","Devices");
AggregationOperation match = Aggregation.match(Criteria.where("clientId").is(clientid));
Aggregation agg = Aggregation.newAggregation(match, lookup);
AggregationResults<OrderedDeviceByOrderId> results = mongoTemplate.aggregate(agg, "order", OrderedDeviceByOrderId.class);
List<OrderedDeviceByOrderId> orderedDeviceByOrderId = results.getMappedResults();
orderedDeviceByOrderId.forEach(s -> System.out.println(s));
return orderedDeviceByOrderId;
}