简体   繁体   中英

How to count elements matching certain condition in array field and insert into projection with MongoDB java driver

I have made Aggregations.lookup from two collections, that returned list of stores, each having field of array of displayed products, with each product having its price. The task is to receive projection, showing for each shop:

  1. Total amount of products
  2. Avg products price
  3. Price of the most expensive and the cheapest product
  4. And amount of products with price less than 10

The problem is I don't understand exactly how to fulfill the fourth task, cause i don't quite understand the syntax of projections.computed. Here's code

AggregateIterable<Document> it = storesCollection.aggregate(Arrays.asList(
                lookup("Products", "products", "name", "products_displayed")
                , project(fields(
                        include("name")
                        , excludeId()
                        , computed("total", computed("$size", "$products_displayed")) //total
                        , computed("avgprice", computed("$avg", "$products_displayed.price")) //avg
                        , computed("maxprice", computed("$max", "$products_displayed.price")) //max
                        , computed("minprice", computed("$min", "$products_displayed.price")) //min
                        , computed("total lt 10", computed("$size", )) // this is the problem: count total amount of products with prices less than 10
                    )
                )
            )
        );

Well, here's how you can do this:

AggregateIterable<Document> it = storesCollection.aggregate(
                Arrays.asList(
                    lookup("Products", "products", "name", "products_displayed")
                    , project(fields(
                            excludeId()
                            , include("name")
                            , computed("total", computed("$size", "$products_displayed"))
                            , computed("avgprice", computed("$avg", "$products_displayed.price"))
                            , computed("maxprice", computed("$max", "$products_displayed.price"))
                            , computed("minprice", computed("$min", "$products_displayed.price"))
                            , computed("total_lt_100", computed("$size",
                                    eq("$filter", and(
                                                    eq("input", "$products_displayed"),
                                                    eq("as", "item"),
                                                    lt("cond", Arrays.asList("$$item.price", 100))
                                            )
                                    )))
                            )
                    )
                )
        );

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