从Java驱动程序传递多个$ Unwind对象 [英] Passing more than one $Unwind object from java driver

查看:259
本文介绍了从Java驱动程序传递多个$ Unwind对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mongo java驱动程序采用var args作为聚合方法,我有一个API,其中动态创建$unwind对象,并且其数量不固定.我如何通过Mongo Java驱动程序聚合方法传递它,因为它需要分别传递每个对象.我尝试通过将所有$unwind对象放入BasicDBList中并通过,但是失败.有人可以帮我解决问题吗?

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?

示例:

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

,但是在运行时创建时,这些展开可能会有所不同.

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

推荐答案

您无需创建BasicDBList.它是这样工作的:

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

这篇关于从Java驱动程序传递多个$ Unwind对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆