简介:
我们这里实现用的是 MongoTemplate来连接mongo
1.这里写一个查询db1的相关代码,传参的话 我们传的是管道里面的一些语法参数
@Autowired
private MongoTemplate mongoTemplate ;
@Override
public List<Document> getListByAggregate(List<Document> aggregateList) {
List<Document> list = new ArrayList<>();
MongoCollection<Document> collection = mongoTemplate.getCollection("db1");
AggregateIterable<Document> result = collection.aggregate(aggregateList);
MongoCursor<Document> cursor = result.iterator();
try {
while (cursor.hasNext()) {
Document document = cursor.next();
list.add(document);
}
} finally {
cursor.close();
}
return list;
}
2.这里是调用实例getListByAggregate()方式传参的示例
public List<Document> getBmToDoList(BMListModel bmListModel) {
List<Document> aggregateList = new ArrayList<>();
// 构建分页
aggregateList.add(new Document("$skip", (long) (bmListModel.getPageSize() * bmListModel.getPageIndex())));
aggregateList.add(new Document("$limit", bmListModel.getPageSize()));
return formEngineInstanceDao.getListByAggregate(aggregateList);
}