我有一份教育机构文件,如下所示:
{ name: ..., addresses: [...], courses: [ {name: ... , duration: ..., tags[...]} ] }
标签有一个String数组。
我正在尝试找到一个内部有一些标签的课程,例如: java , eclipse , struts 等...... < / p>
我的搜索方法如下:
public BasicDBList coordinates(List tags){
BasicDBObject cmdBody = new BasicDBObject("aggregate", "EducationalInstitution");
List<BasicDBObject> pipeline = new ArrayList<BasicDBObject>();
BasicDBObject projectParams = new BasicDBObject();
projectParams.put("name", 1);
projectParams.put("addresses.state", 1);
projectParams.put("addresses.locs", 1);
projectParams.put("courses", 1);
pipeline.add(new BasicDBObject("$project", projectParams));
pipeline.add(new BasicDBObject("$unwind", "$addresses"));
pipeline.add(new BasicDBObject("$unwind", "$courses"));
pipeline.add(new BasicDBObject("$match", new BasicDBObject("courses.tags", new BasicDBObject("$all", tags))));
cmdBody.put("pipeline", pipeline);
return (BasicDBList) getDatastore().getDB().command(cmdBody).get("result");
}
BasicDBObject cmdBody = new BasicDBObject("aggregate", "EducationalInstitution");
List<BasicDBObject> pipeline = new ArrayList<BasicDBObject>();
BasicDBObject projectParams = new BasicDBObject();
projectParams.put("name", 1);
projectParams.put("addresses.state", 1);
projectParams.put("addresses.locs", 1);
projectParams.put("courses", 1);
pipeline.add(new BasicDBObject("$project", projectParams));
pipeline.add(new BasicDBObject("$unwind", "$addresses"));
pipeline.add(new BasicDBObject("$unwind", "$courses"));
pipeline.add(new BasicDBObject("$match", new BasicDBObject("courses.tags", new BasicDBObject("$all", tags))));
cmdBody.put("pipeline", pipeline);
return (BasicDBList) getDatastore().getDB().command(cmdBody).get("result");
}
当我运行时,我收到的结果如下:
{ "_id" : ... , "name" : "X25" , "addresses" : { "state" : "DF" , "locs" : [ -15.806789 , -47.912779]} , "courses" : { "name" : "Microsoft Office Word 2010" , "duration" : 22 , "tags" : [...]}}
{ "_id" : ... , "name" : "X25" , "addresses" : { "state" : "DF" , "locs" : [ -15.806789 , -47.912779]} , "courses" : { "name" : "Microsoft Office Excel 2010" , "duration" : 18 , "tags" : [...]}}
{ "_id" : ... , "name" : "X25" , "addresses" : { "state" : "DF" , "locs" : [ -15.806789 , -47.912779]} , "courses" : { "name" : "Microsoft Office PowerPoint 2010" , "duration" : 14 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "DF" , "locs" : [ -15.797209 , -47.883596]} , "courses" : { "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "DF" , "locs" : [ -15.797209 , -47.883596]} , "courses" : { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "DF" , "locs" : [ -15.797209 , -47.883596]} , "courses" : { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : { "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : { "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}}
我想要的是按照教育机构名称和地址放松的小组课程。像这样:
{ "_id" : ... , "name" : "X25" , "addresses" : { "state" : "DF" , "locs" : [ -15.806789 , -47.912779]} , "courses" : { "name" : "Microsoft Office Excel 2010" , "duration" : 18 , "tags" : [...]}}
{ "_id" : ... , "name" : "X25" , "addresses" : { "state" : "DF" , "locs" : [ -15.806789 , -47.912779]} , "courses" : { "name" : "Microsoft Office PowerPoint 2010" , "duration" : 14 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "DF" , "locs" : [ -15.797209 , -47.883596]} , "courses" : { "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "DF" , "locs" : [ -15.797209 , -47.883596]} , "courses" : { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "DF" , "locs" : [ -15.797209 , -47.883596]} , "courses" : { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : { "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : { "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}}
{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}}
{ "_id" : ... , "name" : "X25" , "addresses" : { "state" : "DF" , "locs" : [ -15.806789 , -47.912779]} , "courses" : [{ "name" : "Microsoft Office Word 2010" , "duration" : 22 , "tags" : [...]}, { "name" : "Microsoft Office Excel 2010" , "duration" : 18 , "tags" : [...]}, { "name" : "Microsoft Office PowerPoint 2010" , "duration" : 14 , "tags" : [...]}]}
我读到了关于$ push和尝试实现但是没有成功。我尝试在管道变量中添加BasicDBObject并在cmdBody中附加命令。
有人为同样的问题通过了吗?
答案 0 :(得分:1)
您可以在shell中使用$ push运算符和$ group管道运算符:
db.t.aggregate([{$unwind:'$b'},{$unwind:'$c'},{$group:{_id:'$b',cs:{$push:'$c'}}}])
在JAVA的情况下,例如:
pipeline.add(new BasicDBObject("$group", new BasicDBObject(new BasicDBObject("_id", groupParams)).append("courses", new BasicDBObject("$push", "$courses"))));
如果您严格使用与您描述的格式相同的格式,则最后可以包含$ project步骤,您可以重新格式化结果文档。