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

シェルでは、次のように $group パイプライン演算子とともに $push 演算子を使用できます。

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 に答える