如何使用spring

时间:2018-02-15 16:37:30

标签: mongodb mongodb-query spring-data-mongodb

我试图从mongoDB的集合中获取所有文档,我正在使用spring。

MongoTemplate  mongoOperation = SpringMongoConfig1.mongoTemplate()

在monngoOperation中,我没有找到任何返回集合中所有文档的方法。 任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

如果要查找JavaDocumentName的所有文档(java中的任何集合名称)

List<JavaDocumentName> listRes = mongoOperation.findAll(JavaDocumentName.class);

答案 1 :(得分:0)

您可以这样做:

//If you don't need connection's configures. i.e. your mongo is in you localhost at 127.0.0.1:27017
MongoClient cliente = new MongoClient(); //to connect into a mongo DB

MongoDatabase mongoDb = client.getDatabase("DBname");  //get database, where DBname is the name of your database

MongoCollection<Document> robosCollection = mongoDb.getCollection("collectionName"); //get the name of the collection that you want

MongoCursor<Document> cursor =  robosCollection.find().cursor();//Mongo Cursor interface implementing the iterator protocol

    cursor.forEachRemaining(System.out::println); //print all documents in Collection using method reference