一、注入MongoDatabaseFactory

@Autowired
private MongoDatabaseFactory mongo;

二、使用MongoDatabaseFactory

@RequestMapping("/test2")
public List<Document> test2(){
MongoDatabase mongoDatabase = mongo.getMongoDatabase();
MongoCollection<Document> documents = mongoDatabase.getCollection("userInfo");
List<Document> documentList = new ArrayList<>();
documents.find().forEach(doc -> documentList.add(doc));

return documentList;

}

##########################################
接口直接返回MongoCollection类型报错。