javamongodbspring-bootmongotemplate

MongoTemplate equivalent of match all Criteria


In Mongosh I can pass an empty object {} to match all documents. I cannot find an equivalent in Java MongoTemplate API. The closest is:

Query query = Query.query(Criteria.where("_id").exists(true));
List<Hierarchy> hierarchies = mongoTemplate.find(query, Hierarchy.class);

which translates as

{ "_id" : { "$exists" : true}}

Is there some better way?


Solution

  • you can use the following code to retrieve all documents from a collection using the MongoTemplate API in Java:

    List<Hierarchy> hierarchies = mongoTemplate.find(new Query(),Hierarchy.class);