自定义字段与MONGO进行汇总

时间:2018-11-15 13:27:29

标签: java mongodb

是否可以使用java和mongoDB将2个自定义字段(我的集合中不存在)添加到聚合组查询中?我的方法在下面,但不起作用

    Double custom_calculated_field1 = 0d;
    Double custom_calculated_field2 = 0d;
    Document customGroup_fields = new Document();
    customGroup_fields.append("customer_id", "$customer_id");
    //custom_calculated fields
    customGroup_fields.append("custom_calculated_field1", custom_calculated_field1);
    customGroup_fields.append("custom_calculated_field2", custom_calculated_field2);
         AggregateIterable<Document> customers_results = customerID.aggregate(
            Arrays.asList(
                    Aggregates.match(values),
                    Aggregates.group(customGroup_fields)
            ));

1 个答案:

答案 0 :(得分:0)

这是您构建聚合方式的问题。您可以在以下网站上的Mongo聚合文档中了解更多相关信息:https://docs.mongodb.com/manual/aggregation/

有效地,您假定管道知道您要执行的操作。但是,管道的工作方式与unix管道|的工作方式非常相似。它执行任何命令并将输出传递给下一个命令。因此,您需要告诉它您的比赛是比赛,而比赛组是比赛组。我有一个DocumentDBObject的示例,其中DBObject是我认为是最新的方法。考虑以下示例:

  @Test
    public void testAggProjection() { 
        DBCollection collection = template.getCollection("test-collection");

        DBObject ob = new BasicDBObject(ImmutableMap.of("test", "value1", "something", "here"));
        DBObject ob2 = new BasicDBObject(ImmutableMap.of("test", "value1", "hello", "world"));
        DBObject ob3 = new BasicDBObject(ImmutableMap.of("test", "value2", "other", "fields"));
        WriteResult insert = collection.insert(ob, ob2, ob3);
        Assert.assertEquals(3, insert.getN());

        DBObject match = new BasicDBObject("$match", new BasicDBObject("test", "value1")); // match the test value 
        Map<String, Object> grouping = new HashMap<>();
        grouping.put("_id", "$test");
        grouping.put("my_count", new BasicDBObject("$sum", 1));
        DBObject group = new BasicDBObject("$group", grouping); // group by 

        AggregationOutput aggregate = collection.aggregate(match, group);
        System.out.println(aggregate.results());
        DBObject next = aggregate.results().iterator().next();
        Assert.assertTrue(next.containsField("_id"));
        Assert.assertTrue(next.containsField("my_count"));


        // with documents raw 
        MongoClient mc = (MongoClient) client;
        MongoCollection<Document> collection2 = mc.getDatabase(template.getDb().getName()).getCollection("test-collection");

        Document dob = new Document(ImmutableMap.of("test", "value1", "something", "here"));
        Document dob2 = new Document(ImmutableMap.of("test", "value1", "hello", "world"));
        Document dob3 = new Document(ImmutableMap.of("test", "value2", "other", "fields"));

        collection2.insertMany(Arrays.asList(dob, dob2, dob3));

        long count = collection2.count();
        Assert.assertEquals(3, count);

        Document match2 = new Document("$match", new Document("test", "value1"));
        Document group2 = new Document("$group", ImmutableMap.of("_id", "$test", "my_count", new Document("$sum", 1)));

        AggregateIterable<Document> aggregate2 = collection2.aggregate(Arrays.asList(match2, group2));
        Document next2 = aggregate2.iterator().next();
        Assert.assertTrue(next2.get("_id") != null);
        Assert.assertTrue(next2.get("my_count") != null);
    }

这是一个工作单元测试。这样做(在两种情况下):

  • 创建一个新集合并插入3个文档。
  • 通过一个匹配项,两个匹配的文档以及一个计算发现的对象的组来汇总它们。

对您来说重要的地方在这里:

Document match2 = new Document("$match", new Document("test", "value1"));
        Document group2 = new Document("$group", ImmutableMap.of("_id", "$test", "my_count", new Document("$sum", 1)));

        AggregateIterable<Document> aggregate2 = collection2.aggregate(Arrays.asList(match2, group2));

在我的match2group2文档中,我将聚合执行的阶段定义为$match$group

结果是,Json是:

Document{{_id=value1, my_count=2}}

希望对您有所帮助!

请注意,组内的_id字段是必填字段。

Artur

相关问题