Reputation: 329
I have a project in spring boot and using mongoDb for database.
Now how do make this query work
db.mycoll.aggregate([{ $sample: { size: 1 } }])
How do I convert into custom query using @Query annotation in spring data mongodb?
Upvotes: 0
Views: 405
Reputation: 1001
You can make a custom repo to achieve the same using mongoTemplate.
SampleOperation sample = Aggregation.sample(1);
Aggregation aggregation = newAggregation(sample);
AggregationResults<T> result =
this.mongoTemplate.aggregate(aggregation, "CollectionToSearch","Return-type-object");
return result.getMappedResults();
Upvotes: 1