简体   繁体   English

MongoDB:如何使用Java驱动程序获取包含数组的所有元素?

[英]MongoDB : how to get all elements that contain an array using Java Driver?

How would you write this MongoDB query using Java driver : 您将如何使用Java驱动程序编写此MongoDB查询:

db.customers.find({'arrayName' : {$exists:true}, $where:'this.arrayName.length>0'}) db.customers.find({'arrayName':{$ exists:true},$ where:'this.arrayName.length> 0'})

Cheers, Yann 干杯,扬

To build a query with the Java driver, you substitute any Javascript objects with DBObject 's. 要使用Java驱动程序构建查询,您可以将所有Javascript对象替换为DBObject

DBObject condition = new BasicDBObject();
condition.put("arrayName", new BasicDBObject("$exists", true));
condition.put("$where",  "this.arrayName.length>0");

DBCursor result = yourDatabase.getCollection("customers").find(condition);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM