MongoDB as Java operations
I have a field in mongodb where documents start with words like 1.1., 2.0., 2.1., 3.0., 3.1, etc., but I need to request documents that start with a specific line, like 1.1.or 2.0 ... etc. I don't care that after this 1.1. or 2.0. or. I tried a query like this
BasicDBObject whereQuerylevel = new BasicDBObject();
whereQuerylevel.put("level",new BasicDBObject("$regex", "^1.1."));
myCollection.find(whereQuerylevel);
how to get documents in a field that starts with 1.1. or 2.0. something similar in mongodba?
+3
source to share
1 answer
BasicDBObject regexQuery = new BasicDBObject();
regexQuery.put("level", new BasicDBObject("$regex", "^[1-9].[1-9]");
myCollection.find(regexQuery);
If you need to limit the number in your criteria, you can change the range inside the brackets. those. [1-9] you will get the whole number from 1 to 9, if you want to limit it so that the first number is between 1-3 and the second number after the point was zero or 1, then you can change it to [1 -3]. [0-1]
I would highly recommend reading this interactive regex tutorial
+1
source to share