Conditional query for MONGO in Java

Source: Internet
Author: User

@Override public page<productinfo> findAll (pageable pageable, ProductInfo productinfo) {//Create an action aggregation operations list<aggregationoperation> operations = new arraylist<> ();//Create a conditional class criteria criteria = new            Criteria ();//The commodity status is not NULL if (Productinfo.getproductstatus ()!=null) {//productstatus equals the item status of the query added to the Operation aggregation operations        Operations.add (Aggregation.match (Criteria.and ("Productstatus"). Is (Productinfo.getproductstatus ())));        } long totalcount = 0;  Total pages if (operations!= null && operations.size () > 0) {//Operations aggregation Aggregation Aggregationcount    = Newaggregation (Operations); Query the total number of data bars, return a collection aggregationresults<productinfo> Resultscount = Mongotemplate.aggregate (aggregationcount            , "ProductInfo", Productinfo.class);        TotalCount = Resultscount.getmappedresults (). Size (); }else{//Operation aggregation is empty, query the total number of data bars TotalCount = Mongotemplate.findall (productinfo.class). SiZe (); }//operation aggregation Add pages multiplied by each page size equals total number of pages Operations.add (Aggregation.skip ((Long) (Pageable.getpagenumber ()) * Pageable.getpagesize ()) );//Operation aggregation Add returns a page of data Operations.add (Aggregation.limit (Pageable.getpagesize ()));//Operation aggregation Add sort by solvecount descending//oper        Ations.add (Aggregation.sort (Sort.Direction.DESC, "Solvecount")); Aggregation Aggregation = newaggregation (Operations);//query data by condition aggregationresults<productinfo> results = Mon Gotemplate.aggregate (Aggregation, "ProductInfo", productinfo.class);//The map and paging information and total number of records returned results return to new PAGEIMPL&LT;&GT    ;(results.getmappedresults (), pageable,totalcount); }

  

Conditional query for MONGO in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.