0

我有一个教育机构文件,看起来像这样:

{ name: ..., addresses: [...], courses: [ {name: ... , duration: ..., tags[...]} ] }

tags有一个 String 数组。

我正在尝试查找其中包含一些标签的课程,例如:javaeclipsestruts等等......

我的搜索方法如下所示:

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");
}

当我运行它时,我收到如下结果:

{ "_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 Word 2010" , "duration" : 22 , "tags" : [...]}, { "name" : "Microsoft Office Excel 2010" , "duration" : 18 , "tags" : [...]}, { "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" : [...]}, { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}, { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}]}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "PR" , "locs" : [ -25.431803 , -49.279532]} , "courses" : [{ "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}, { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}, { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}]}

{ "_id" : ... , "name" : "ENG" , "addresses" : { "state" : "SP" , "locs" : [ -23.574942 , -46.71048]} , "courses" : [{ "name" : "MS Visio" , "duration" : 0 , "tags" : [...]}, { "name" : "Acrobat Professional" , "duration" : 12 , "tags" : [...]}, { "name" : "Framemaker" , "duration" : 0 , "tags" : [...]}]}

我阅读了 $push 并尝试实施但没有成功。我尝试在管道变量中添加 BasicDBObject 并将命令附加到 cmdBody 中。

有人通过类似的问题吗?

4

1 回答 1

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 步骤,您可以重新格式化结果文档。

于 2013-08-26T11:48:37.513 回答