0

Using MongoDB Java driver, I would like to find all entries where an array field contains at least 2 entries. What I have tried:

myCollection.find(new Document("arrayFieldname.length", new Document("$gt", 1)));

Unfortunately this returns no entry.

Should the approach be different?

Many thanks, Tom

1 Answer 1

2

Use can the $size operator, but that doesn't work for ranges.

The best thing to do is to add a field to the document that tracks the array size, and query based on that. The bonus with that approach is that you could index on that field.

Sign up to request clarification or add additional context in comments.

1 Comment

But how to query in Java?

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.