Mongodb - 按数组元素分组

时间:2017-12-01 12:07:09

标签: mongodb

我正在尝试使用数组内的元素对JSON数据进行分组。 我认为 public class SampleClass { private WebDriver driver; @BeforeTest public void setUp() { System.setProperty("webdriver.chrome.driver","C:\\Users\\AK5040691\\Desktop\\IE driver\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.navigate().to("http://executeautomation.com/blog/custom-testng-library-for-appium/#more-1562"); } //@Parameters({"searchText1","searchText2"}) //@Test public void searchText(String text1, String text2) { driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); driver.findElement(By.className("search-field")).sendKeys(text1); driver.findElement(By.className("search-field")).clear(); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); driver.findElement(By.className("search-field")).sendKeys(text2); } } 会奏效。但我需要编写自定义函数(reduce函数)而不是内置聚合运算符/表达式。有什么建议吗?

示例数据:

db.collection.aggregate

我的代码:

{
    "name" : "Person Name",
    "age" : 50,
    "expense" : [
        {
            "category" : "food",
            "date" : "2017-01-01",
            "amount" : 100
        }, 
        {
            "category" : "travel",
            "date" : "2017-01-02",
            "amount" : 200
        }
    ]
}

其中,db.collection.group({ key: { "expense.category": 1 }, initial: { total : 0 }, reduce: function( curr, result ) { result.total += curr.amount; } }) 对我不起作用。

0 个答案:

没有答案