简体   繁体   中英

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

How would you write this MongoDB query using Java driver :

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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