0

How would you write this MongoDB query using Java driver :

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

Cheers, Yann

2
  • 2
    I would not use this query - it's a bad idea to use $where. Do a regular find with {arrayName.0:{$exists:true}} Commented Aug 5, 2014 at 13:42
  • 1
    Did you have a look at the API doc? Maybe you should be more precise if something there is not clear to you or does not work when you try. Commented Aug 5, 2014 at 14:10

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.