异常:FieldPath'C6'不以mongo Java中的$“开头

时间:2015-06-24 13:20:16

标签: java mongodb aggregation-framework

我正在使用Mongodb处理Java。我试图在某些条件下选择某些记录,包括group by。我使用的代码如下。

DBObject wherequery = new BasciDBOBject();
wherequery.put("deviceID", C6);
wherequery.put("reqTime", new BasicDBObject(
    "$lt", sometime)
    .append("$gt", someothertime));
DBObject project = new BasicDBObject("$project",
    wherequery);
DBObject groupFields = new BasicDBObject("_id",
    "$requestID");
DBObject group = new BasicDBObject("$group", groupFields);
AggregationOutput output = collection.aggregate(project, group);

当我运行此代码时,我收到以下错误:

exception: FieldPath 'C6' doesn't start with $"

这个错误意味着什么?它表示我使用的deviceID C6。我错过了什么或我错了什么?

请帮帮我

2 个答案:

答案 0 :(得分:2)

如果要选择符合指定条件的文档,则应使用$match运算符作为第一个pipline阶段,而不是$project

答案 1 :(得分:1)

将您的wherequery添加到$matchproject使用不同的DBObject支票代码,如下所示:

DBObject wherequery = new BasciDBOBject();
wherequery.put("deviceID",C6 );
wherequery.put("reqTime", new BasicDBObject(
    "$lt", sometime)
    .append("$gt", someothertime));
DBObject match = new BasicDBObject("$match",
    wherequery);
DBObject groupFields = new BasicDBObject("_id",
    "$requestID");
DBObject group = new BasicDBObject("$group", groupFields);
//for projecting data 
DBObject projectData = new BasciDBOBject();
projectData.put("deviceID", "$deviceID"); // projectData.put("deviceID", 1); this also work
projectData.put("reqTime", "$reqTime");
DBObject project = new BasicDBObject("$project",
    projectData);
AggregationOutput output = collection.aggregate(match, group,project);

有关详情,请查看 Mongo Java driver for aggregation