0

The mongo java driver takes var args for aggregate method, I have an API in which $unwind objects get's created dynamically and its number is not fixed. how can I pass it through Mongo Java driver aggregate method, as it needs each object to be passed separately. I tried passing putting all the $unwind object in a BasicDBList and pass, but it fails. Can someone help me with some work around?

example:

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

, but these unwind may vary as it is getting created at runtime.

Simulant
  • 19,190
  • 8
  • 63
  • 98
Phalguni Mukherjee
  • 623
  • 3
  • 11
  • 29
  • you could do separate unwind operations in the pipeline, one after another. But you should realize this is going to be a performance nightmare. – Jayz Aug 03 '13 at 03:56
  • Performance overhead, cannot do that – Phalguni Mukherjee Aug 03 '13 at 10:29
  • can you give details of how your collection is structured? is item2 a list in items? if items and item2 are separate lists in your collection, why would you need to unwind both items and item2? – Jayz Aug 03 '13 at 10:42

1 Answers1

0

you don't need to create a BasicDBList. This is how it works:

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);
Jayz
  • 1,174
  • 2
  • 19
  • 43