0

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?

2
  • @Query annotation supports regular find queries not aggregate queries. Commented Oct 25, 2018 at 15:40
  • @Veeram any idea for this then - db.person.find().limit(1).skip(10) Commented Oct 25, 2018 at 15:45

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.