Java Spring数据MongoDB聚合与日期和平均值匹配

Java Spring数据MongoDB聚合与日期和平均值匹配,java,mongodb,Java,Mongodb,我使用mongoDB java驱动程序通过聚合框架查询日期范围和平均值之间的事务。这是我的mongo查询(有效): 我用来进行查询的java代码: public Double getAverageTimeWithPeriod(){ MongoCollection<Document> dbCollection = mongoTemplate.getCollection(ChatUserCampaign.COLLECTION_NAME); Date fromDate =

我使用mongoDB java驱动程序通过聚合框架查询日期范围和平均值之间的事务。这是我的mongo查询(有效):

我用来进行查询的java代码:

public Double getAverageTimeWithPeriod(){
    MongoCollection<Document> dbCollection = mongoTemplate.getCollection(ChatUserCampaign.COLLECTION_NAME);

    Date fromDate = LocalDate.parse("2019-10-01").toDate();
    Date toDate = LocalDate.parse("2020-03-13").toDate();

    BasicDBObject match = new BasicDBObject("$match",
            new BasicDBObject("created",
                    new BasicDBObject("$gte", fromDate/*getDate("01/10/2019")*/).append("$lt", toDate/*getDate("20/10/2019")*/)));
    BasicDBObject group = new BasicDBObject("$group",
            new BasicDBObject ("_id",
                    new BsonField("averageTime",
                    new BsonDocument("$avg,",
                            new BsonString("$details.nostradamusOfferCalculatingTime")))));

    List<Bson> aggregators = null;
    assert false;

    aggregators.add(match);
    aggregators.add(group);
    AggregateIterable<Document> output = dbCollection.aggregate(aggregators);
    //AggregationOutput output1 = dbCollection.aggregate(Arrays.asList(match,group));
    Document result = output.first();
    return result.getDouble("averageTime");
}
public双精度getAverageTimeWithPeriod(){
MongoCollection dbCollection=mongoTemplate.getCollection(ChatUserCampaign.COLLECTION\u NAME);
Date fromDate=LocalDate.parse(“2019-10-01”).toDate();
Date toDate=LocalDate.parse(“2020-03-13”).toDate();
BasicDBObject match=新的BasicDBObject(“$match”,
新的BasicDBObject(“已创建”,
新的BasicDBObject($gte),fromDate/*getDate(“2019年10月1日”)*/)。追加($lt,toDate/*getDate(“2019年10月20日”)*/);
BasicDBObject组=新的BasicDBObject(“$group”,
新的BasicDBObject(“\u id”,
新建B字段(“平均时间”,
新BsonDocument($avg,”,
新的BsonString($details.nostradamusOfferCalculatingTime“));
列表聚合器=null;
断言错误;
聚合器。添加(匹配);
聚合器。添加(组);
AggregateIterable输出=dbCollection.aggregate(聚合器);
//AggregationOutput output1=dbCollection.aggregate(Arrays.asList(match,group));
文档结果=output.first();
返回结果.getDouble(“averageTime”);
}

但是这个jaja查询返回的结果是空的(与:
aggregators.add(match);
-我得到了NullPointerException)。

Mongo不理解
01/10/2019
格式,而是理解ISO格式。因此,您必须使用类似于ISODate(“2015-06-17T10:03:46Z”)的东西来代替
fromDate
toDate
<代码>日期或
本地日期
不起作用。

日期正常。此代码:
datefromdate=LocalDate.parse(“2019-10-01”).toDate()工作正常。所以这没问题。
public Double getAverageTimeWithPeriod(){
    MongoCollection<Document> dbCollection = mongoTemplate.getCollection(ChatUserCampaign.COLLECTION_NAME);

    Date fromDate = LocalDate.parse("2019-10-01").toDate();
    Date toDate = LocalDate.parse("2020-03-13").toDate();

    BasicDBObject match = new BasicDBObject("$match",
            new BasicDBObject("created",
                    new BasicDBObject("$gte", fromDate/*getDate("01/10/2019")*/).append("$lt", toDate/*getDate("20/10/2019")*/)));
    BasicDBObject group = new BasicDBObject("$group",
            new BasicDBObject ("_id",
                    new BsonField("averageTime",
                    new BsonDocument("$avg,",
                            new BsonString("$details.nostradamusOfferCalculatingTime")))));

    List<Bson> aggregators = null;
    assert false;

    aggregators.add(match);
    aggregators.add(group);
    AggregateIterable<Document> output = dbCollection.aggregate(aggregators);
    //AggregationOutput output1 = dbCollection.aggregate(Arrays.asList(match,group));
    Document result = output.first();
    return result.getDouble("averageTime");
}