Anton Putov
Anton Putov

Reputation: 1981

java mongodb morphia : not contains

How can I write query with morphia (or without it) to match all documents whose collections don't contain a child with some value? In my case the collection holds strings, something like this:

    myClass{
          Collection<String> collection;
          ...
    }

If I would like to search documents whose collection holds some value I would write something like this:

    query.and(
         ......
         query.criteria("collection").contains("search string").) 
        // or possible hasThisElement("search string")
    );

I can try to use hasNoneOf but I am worry about perfomance (it is designed for a collection input, and I have a single argument) and I would like to hear other suggestions.I am looking for something like notContains. Thanks.

Edit

Possible morphia wiki can be helpfull.

Upvotes: 1

Views: 1234

Answers (1)

xeraa
xeraa

Reputation: 10859

hasNoneOf gets converted to $nin. I think that should be reasonably fast if you index the collection and each collection only contains a few dozen values.

The answer for $in should apply for your situation as well: https://stackoverflow.com/a/4961447/573153 So I'd think your fine with this approach. If in doubt, do a mini-benchmark yourself.

Upvotes: 2

Related Questions