简体   繁体   中英

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM