ts178
ts178

Reputation: 329

Spring data MongoDb custom query

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

Answers (1)

Nishant Bhardwaz
Nishant Bhardwaz

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

Related Questions