Beacause fields inside mongo collection is changes over object to object. in object there is 4 fields in other there is 8 fields so how we get all the collection data.
Beacause fields inside mongo collection is changes over object to object. in object there is 4 fields in other there is 8 fields so how we get all the collection data.
you can get the documents like this without jpa repository
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.web.bind.annotation.GetMapping;
@Autowired
MongoTemplate mongoTemplate;
@GetMapping("/url")
public List<JSONObject> getList() {
ProjectionOperation project = project().andExclude("_id");
Aggregation aggregate = newAggregation(project);
AggregationResults<JSONObject> aggregationResult = mongoTemplate.aggregate(aggregate,"youCollectionName", JSONObject.class);
List<JSONObject> result = aggregationResult.getMappedResults();
return result;
}
You can use MongoDB Spring Data's MongoTemplate API methods as shown below.
The MongoTemplate class's findAll method can be used. The method requires that entityClass be specified. If you don't have an entity class defined, a generic org.bson.Document class can be specified (see example code below).
public <T> List<T> `findAll`(Class<T> entityClass, String collectionName)
Query for a list of objects of type T from the specified collection.
The object is converted from the MongoDB native representation using an instance of MongoConverter. Unless configured otherwise, an instance of MappingMongoConverter will be used. If your collection does not contain a homogeneous collection of types, this operation will not be an efficient way to map objects since the test for class type is done in the client and not on the server.
Example:
MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "test");
List<Document> list = mongoOps.findAll(Document.class, "person");
list.forEach(doc -> System.out.println(doc.toJson()));
In the above code, person collection is read; and the collection has documents with different fields. For input documents:
{ "_id" : 1, "fld" : "str-1" }
{ "_id" : 3, "fld" : "str-3", "fld2" : 13 }
{ "_id" : 10, "dt" : ISODate("2020-03-07T03:08:49.855Z") }
The output is:
{"_id": 1.0, "fld": "str-1"}
{"_id": 3.0, "fld": "str-3", "fld2": 13.0}
{"_id": 10.0, "dt": {"$date": 1583550529855}}