I have JSON documents in MongoDB like these
{
"os" : "Windows 10"
}
{
"os" : "WINDOWS 8"
}
{
"os" : "UBunTu 18.04"
}
how can I query on os
with case insensitivity and contains using Spring MongoTemplate so that I can pass ["windows", "ubuntu"]
and get all three documents from DB
I tried something like this but did not work.
String osRegEx = String.join(" | ", new String[]{"windows", "ubuntu"});
query.addCriteria(Criteria.where("os").regex(osRegEx, "i"));
It worked in this way...
String osRegEx = String.join("|", new String[]{"windows.*", "ubuntu.*"});
query.addCriteria(Criteria.where("os").regex("("+osRegEx+")", "i"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With