'Java MongoDB – Find objects that contain a certain string returning no results
I'm using java's mongodb driver and trying to get all items where the "text" contains the term "Example". However, it just returns no items, no matter what I try. Even using "a" instead of "Example" (which 100% is included in most results) returns 0.
What am I doing wrong here?
db.getCollection("Items").find(in("text", "Example"))
Also tried passing a List instead of "Example", which also didn't work.
Any suggestions?
Thanks!
Solution 1:[1]
Using collections a solution could look like this one:
db.getCollection("Items").find(Filters.in("text", ".*Example.*"));
You need a regex because (from the JavaDoc) in:
Creates a filter that matches all documents where the value of a field **equals** any value in the list of specified values.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | blafoo |
