Java 使用相同的聚合MongoDB获得不同的结果
我正在Robo3T中进行聚合:Java 使用相同的聚合MongoDB获得不同的结果,java,mongodb,aggregation-framework,Java,Mongodb,Aggregation Framework,我正在Robo3T中进行聚合: db.getCollection('Tweets').aggregate([ {"$match": {"ownerId": "5c8d2ce116c7f85377df590a"}} , {"$match": {"topic": "topic_A"}} , {"$match": {"timestamp" : { "$gte" : ISODate("2020-02-01T00:00:00.000Z") , "
db.getCollection('Tweets').aggregate([
{"$match": {"ownerId": "5c8d2ce116c7f85377df590a"}}
, {"$match": {"topic": "topic_A"}}
, {"$match": {"timestamp" : { "$gte" : ISODate("2020-02-01T00:00:00.000Z")
, "$lte" : ISODate("2020-02-05T23:59:59.000Z")
}
}
}
,{"$count": "totalData"}
])
文件总数为5742份。
现在,我正在使用MongoDB驱动程序编写的一个简短程序中进行相同的聚合
代码如下:
public void countMonthData() {
LocalDateTime start = YearMonth.now().atDay(1).atStartOfDay();
LocalDateTime end = YearMonth.now().atDay(5).atStartOfDay().plusSeconds(86399);
MatchOperation matchOperation = Aggregation.match(new Criteria("ownerId").is("5c8d2ce116c7f85377df590a"));
MatchOperation matchTopic = Aggregation.match(new Criteria("topic").is("topic_A"));
MatchOperation matchDates = Aggregation.match(new Criteria("timestamp").gte(start).lte(end));
Aggregation agg = Aggregation.newAggregation(matchOperation, matchTopic, matchDates);
System.out.println(agg);
AggregationResults<Document> result = mongoTemplate.aggregate(agg, Tweets.class, Document.class);
System.out.println("size: " + result.getMappedResults().size());
}
时区差?您似乎正在使用Robo3T中的UTC和司机的当地时间。有相同的想法,所以我更改了时区,但得到了相同的结果:(尝试运行数据库命令,将
profile
和slowms
设置为0,然后从驱动程序和Robo3T运行聚合。您应该会在mongod日志中看到一个条目,其中包含已运行聚合的详细信息。希望您能够看到其中的差异。
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.TimeZone;
@Configuration
public class LocaleConfig {
@PostConstruct
public void init() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.out.println("Date in UTC: " + new Date().toString());
}
}