我在mongodb中有一个集合,其中包含以下格式的条目:
{
"_id":ObjectId("54fb1dec44ae41b348e22545"),
"dateLog": ISODate("2015-03-07T16:00:00 Z"),
"totalHours":0,
"projects":[
{
"client":"BB",
"name":"Training",
"component":"ElasticSearch",
"region":"test",
"hours":1
},
{
"client":"PAC",
"name":"TSO",
"component":"feature1",
"region":"a",
"hours":2
},
{
"client":"PAC",
"name":"TSO",
"component":"feature1",
"region":"b",
"hours":2
}
],
"userName":"guest"
}
我想执行聚合查询并计算所有条目的projects.client,projects.name和projects.component的数量。 到目前为止,我的聚合查询类似于:
db.worklog.aggregate(
{
$match:{
userName:"guest"
}
},
{
$project:{
"projects.client":1,
"projects.name":1,
"projects.component":1
}
},
{
$group:{
_id:"$projects.client",
sum:{
$sum:"$projects.client"
}
}
}
)
但结果为{ "_id" : [ "BB", "PAC", "PAC" ], "sum" : 0 }
我想要算的是:
{ "_id" : { "projects.component" : "ElasticSearch", "projects.client" : "BB" , "projects.name" : "Training"} , "count" : 1}
{ "_id" : { "projects.component" : "feature1", "projects.client" : "PAC" , "projects.name" : "TSO"} , "count" : 1}
正确的查询是什么?
答案 0 :(得分:0)
通过使用下面的mongo java驱动程序片段,我想我可以得到所需的结果:
DBObject match = new BasicDBObject("$match", new BasicDBObject("userName", userName));
DBObject fields = new BasicDBObject("compkey", 1);
fields.put("projects.client", 1);
fields.put("projects.component", 1);
fields.put("projects.name", 1);
fields.put("_id", 0);
DBObject project = new BasicDBObject("$project", fields );
Map<String, Object> dbObjIdMap = new HashMap<String, Object>();
dbObjIdMap.put("projects.client", "$projects.client");
dbObjIdMap.put("projects.component", "$projects.component");
dbObjIdMap.put("projects.name", "$projects.name");
DBObject groupFields = new BasicDBObject( "_id", new BasicDBObject(dbObjIdMap));
groupFields.put("count", new BasicDBObject( "$sum", 1));
DBObject group = new BasicDBObject("$group", groupFields);
DBObject sort = new BasicDBObject("$sort", new BasicDBObject("count", -1));
DBObject limit = new BasicDBObject("$limit", count);
List<DBObject> myList = new ArrayList<DBObject>();
if (!userName.equals("all"))
myList.add(match);
myList.add(project);
myList.add(group);
myList.add(sort);
myList.add(limit);
List<DBObject> pipeline = myList;
AggregationOutput output = coll.aggregate(pipeline);
Json结果:
{ "_id" : { "projects.component" : [ "Vacation/Paid Time Off"] , "projects.client" : [ "Internal"] , "projects.name" : [ "Vacation"]} , "count" : 7}
{ "_id" : { "projects.component" : [ "Vacation/Paid Time Off"] , "projects.client" : [ "Internal"] , "projects.name" : [ "Vacation"]} , "count" : 50}
{ "_id" : { "projects.component" : [ "Daily Meeting" , "Jira-Ticket"] , "projects.client" : [ "Internal" , "Visualize"] , "projects.name" : [ "Meetings" , "Nectar Sprint 1"]} , "count" : 6}
{ "_id" : { "projects.component" : [ "Generic"] , "projects.client" : [ "BB"] , "projects.name" : [ "BBTO"]} , "count" : 45}
{ "_id" : { "projects.component" : [ ] , "projects.client" : [ "BB"] , "projects.name" : [ "BBMC"]} , "count" : 43}
{ "_id" : { "projects.component" : [ "Technology Status"] , "projects.client" : [ "BB"] , "projects.name" : [ "Meetings"]} , "count" : 42}
{ "_id" : { "projects.component" : [ "Daily Meeting" , "Nectar 1"] , "projects.client" : [ "Internal" , "Visualize"] , "projects.name" : [ "Meetings" , "Visualize"]} , "count" : 28}
{ "_id" : { "projects.component" : [ "Daily Meeting"] , "projects.client" : [ "Internal"] , "projects.name" : [ "Meetings"]} , "count" : 25}