mongodb java groupby查询转换
mongodb新手。我一直在玩MongoJava驱动程序,到目前为止一切顺利 我已成功使用DBCollection.aggregate–我通过了:mongodb java groupby查询转换,java,mongodb,Java,Mongodb,mongodb新手。我一直在玩MongoJava驱动程序,到目前为止一切顺利 我已成功使用DBCollection.aggregate–我通过了: $match $project $group $sort 例如,我已经制定了如下标准: Map<String, Object> match_criteria; match_criteria.put(“properties.type”, “monuments”); DBObject match = new BasicDBObject()
$match
$project
$group
$sort
例如,我已经制定了如下标准:
Map<String, Object> match_criteria;
match_criteria.put(“properties.type”, “monuments”);
DBObject match = new BasicDBObject();
match.putAll(match_criteria); //was just setting from Map
DBObject match_dbobj = new BasicDBObject("$match", match);
.
.
.
.
.in the same way was populating $project, $group and $sort
但现在使用这种方法转换以下mongo shell查询时遇到问题:
db.test.aggregate({
$match:{ ---> still can pass with Map
"properties.type":'monuments',
}
},
{
$project:{ ---> still can pass with Map
"props.country":1,
"attrs.time":1,
"attrs.status":1
}
},
{
$group:{ ---> trying to find a way to handle $group from java code
_id:{
status:'$attrs.status',
country:'$props.country'
},
time:{
$last:'$attrs.time'
}
}
}
)
如何转换$group-我不能再使用地图了。有什么想法吗?在您的示例中,$group是一个嵌套的DBObject,因此创建它的方法是:
// Create the group sub documents
DBObject idDocument=new BasicDBObject("status","$attrs.status").append("country","$props.country");
DBObject timeDocument=new BasicDBObject("$last","$attrs.time");
// Create the group document
DBObject groupDocument=new BasicDBObject("_id",idDocument).append("time",timeDocument);
// Create the whole group statement
DBObject group=new BasicDBObject("$group", groupDocument);
使用计算出的into Aggregates.project。
// Create the group sub documents
DBObject idDocument=new BasicDBObject("status","$attrs.status").append("country","$props.country");
DBObject timeDocument=new BasicDBObject("$last","$attrs.time");
// Create the group document
DBObject groupDocument=new BasicDBObject("_id",idDocument).append("time",timeDocument);
// Create the whole group statement
DBObject group=new BasicDBObject("$group", groupDocument);