0

mongo Java ドライバーは、集計メソッドに var args を使用します。$unwindオブジェクトが動的に作成され、その数が固定されていない API があります。各オブジェクトを個別に渡す必要があるため、Mongo Java ドライバーの集計メソッドを介して渡すにはどうすればよいですか。すべての$unwindオブジェクトを BasicDBList に入れてパスしようとしましたが、失敗します。誰かが回避策を手伝ってくれますか?

例:

db.foo.aggregate({$unwind:items},{$unwind:item2})

、ただし、これらのアンワインドは実行時に作成されるため、異なる場合があります。

4

1 に答える 1

0

BasicDBList を作成する必要はありません。これがどのように機能するかです:

List<DBObject> unwindItems = new ArrayList<>();

if(<item2 is not null>){ //pseudo code
  DBObject unwindItem1 = new BasicDBObject("$unwind", "$item1");
  unwindItems.add(unwindItem1);
}
if(<item2 is not null>){ //pseudo code
  DBObject unwindItem2 = new BasicDBObject("$unwind", "$item2");
  unwindItems.add(unwindItem2);
}
//add any other dbObject in the list, it need not be an unwind operation, it could be match, project, group etc.

DBObject command = new BasicDBObject("aggregate", "foo");
command.put("pipeline", dbObjects);
于 2013-08-03T10:51:08.857 に答える