Yann
Yann

Reputation: 135

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

Upvotes: 0

Views: 187

Answers (1)

Philipp
Philipp

Reputation: 69663

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

Upvotes: 1

Related Questions