简体   繁体   English

如何使用 MongoDB java 驱动程序计算数组字段中匹配特定条件的元素并插入投影

[英]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.我从两个 collections 进行了 Aggregations.lookup,返回了商店列表,每个商店都有显示产品数组的字段,每个产品都有价格。 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以及价格低于 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.问题是我不完全了解如何完成第四个任务,因为我不太了解 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))
                                            )
                                    )))
                            )
                    )
                )
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB 如何使用 Java 驱动程序计算数组元素 - MongoDB how to count array elements with Java driver MongoDB:使用Java驱动程序获取数组字段的元素 - MongoDB: get elements of array field with java driver MongoDB:使用Java驱动程序在具有给定属性的数组中查找匹配元素 - Mongodb : find matching elements in an array with given attributes using java driver MongoDB Java驱动程序如何在投影中使用过滤器 - Mongodb java driver how to use filter in projection mongodb java驱动程序在聚合/投影操作中隐藏id字段 - mongodb java driver hide id field in aggregation/projection operation MongoDB:如何使用Java驱动程序获取包含数组的所有元素? - MongoDB : how to get all elements that contain an array using Java Driver? 如何使用java驱动程序将文档与mongodb中的现有数组元素进行匹配 - How to match a document with existing array elements in mongodb using java driver Mongodb Java驱动程序将数组插入集合 - Mongodb Java Driver Insert Array to Collection Mongodb Java:根据元素的某些条件删除文档中嵌入式数组的元素 - Mongodb Java: remove an element of an embedded array within a document based on certain condition of elements MongoDB Java驱动程序:如何从Java驱动程序插入任何集合 - MongoDB Java Driver: How to insert into any collection from Java Driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM