0

I have my collection like below

{
    "_id" : NumberLong(366),
    "_class" : "com.cts.adpart.domain.DBData",
    "file" : "xyz",
    "sample" : [
                   "a",
                   "b",
                   "c"
             ]
}

I want to search sample array based on list values Suppose now 1. my List is having values "a","c" then i want above document to be returned but, 2. if my list contains "a","d" It should not return above document.

I have tried below code but it is returning me the document even if one match is found.

List<String> sampleList = new ArrayList<String>();
sampleList.add("a");
sampleList.add("d");

query.addCriteria(Criteria.where("sample").in(sampleList));

How to change the query so that it returns document only if all values in list matches the field?

3 Answers 3

1

you should use the $all operator

query.addCriteria(Criteria.where("sample").all(sampleList));
Sign up to request clarification or add additional context in comments.

Comments

0

In order to have exact match, you should query just by the elements in the array. For eg:

{"sample":['a','b','c']}

Refer here

Comments

0

You should use all method of class Criteria. As this document says:

The $all operator selects the documents where the value of a field is an array that contains all the specified elements.

So, you should modify your statement as follows.

query.addCriteria(Criteria.where("sample").all(sampleList));

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.