Skip to content

Commit 0e22a20

Browse files
committed
fixing mongodb where clause
1 parent 8a28de0 commit 0e22a20

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/main/java/picoded/dstack/mongodb/MongoDBStack.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static String getFullConnectionURL(GenericConvertMap<String, Object> conf
5959
int port = config.getInt("port", 27017);
6060
String opts = config.getString("opt_str", "w=majority&retryWrites=true&retryReads=true"
6161
+ "&maxPoolSize=10&compressors=zstd");
62-
// "&readPreference=nearest&readConcernLevel=linearizable"
62+
// "&readPreference=master&readConcernLevel=majority"
6363

6464
// Lets do a logging, for missing read concern if its not configured
6565
if( opts.indexOf("readConcernLevel") < 0 ) {
@@ -76,6 +76,7 @@ public static String getFullConnectionURL(GenericConvertMap<String, Object> conf
7676
//
7777
LOGGER.warning("MongoDB is configured without readConcernLevel, "+
7878
"this is alright for a single node, but `readConcernLevel=linearizable`"+
79+
"or `readPreference=master&readConcernLevel=majority`"+
7980
"is highly recommended for replica clusters to ensure read after write consistency.");
8081
}
8182

src/main/java/picoded/dstack/mongodb/MongoDB_DataObjectMap.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package picoded.dstack.mongodb;
22

33
// Java imports
4+
import java.util.regex.Pattern;
45
import java.util.HashMap;
56
import java.util.HashSet;
67
import java.util.List;
@@ -293,13 +294,12 @@ public Set<String> keySet() {
293294
HashSet<String> ret = new HashSet<String>();
294295

295296
// Lets fetch everything ... D=
296-
FindIterable<Document> search = collection.find();
297-
search = search.projection(Projections.include("_oid"));
297+
DistinctIterable<String> search = collection.distinct("_oid", String.class);
298298

299299
// Lets iterate the search
300-
try (MongoCursor<Document> cursor = search.iterator()) {
300+
try (MongoCursor<String> cursor = search.iterator()) {
301301
while (cursor.hasNext()) {
302-
ret.add(cursor.next().getString("_oid"));
302+
ret.add(cursor.next());
303303
}
304304
}
305305

@@ -362,13 +362,14 @@ static protected Bson queryObjToBsonFilter(Query inQuery) {
362362
if (type == QueryType.LIKE) {
363363
// Because the LIKE operator does not natively exists,
364364
// we will generates its REGEX equivalent
365-
366365
String val = GenericConvert.toString(inQuery.defaultArgumentValue());
367-
val = val.replaceAll("*", "\\*");
366+
367+
// val = val.replaceAll("*", "*");
368+
val = val.replaceAll("*", Pattern.quote("*"));
368369
val = val.replaceAll("%", ".*");
369-
val = val.replaceAll("_", ".+");
370+
val = val.replaceAll("_", "[.]");
370371

371-
return Filters.regex(inQuery.fieldName(), val);
372+
return Filters.regex(inQuery.fieldName(), "^"+val+"$");
372373
}
373374
}
374375

0 commit comments

Comments
 (0)