How to find regex in Vertx mongo client

648 views
Skip to first unread message

Hai Nguyen

unread,
Sep 6, 2015, 4:40:22 AM9/6/15
to vert.x
Hi, I'm trying to my regex in vertx mongo works

In mongoHub, I tried this and it works : db.deals.find({ "alias" : /.*buffet-lau-nuong.*/}).sort({ "_id": 1}).skip(0).limit(30)

In vertx, I tried this and no result. Please help 

JsonObject query = new JsonObject()
.put("alias", "/.*" + searchKeyword + ".*/");


mongo.find("deals", query, res -> {
// error handling
if (res.succeeded()) {
for (JsonObject o : res.result()) {
cLog(o.toString());
}
} else {
res.cause().printStackTrace();
}
});

Hai Nguyen

unread,
Sep 6, 2015, 11:19:28 AM9/6/15
to vert.x
I worked around this case by using the official async mongo java driver from mongodb

collection.find(regex("alias", ".*" + searchKeyword + ".*")).forEach(document -> {

// do something

}, (aVoid, throwable) -> {


// Next step



});

Michel Guillet

unread,
Sep 7, 2015, 9:09:23 AM9/7/15
to ve...@googlegroups.com
This query:

JsonObject query = new JsonObject()
        .put("alias", "/.*" + searchKeyword + ".*/");


This doesn’t trigger the use of regex.

Try this:
JsonObject query = new JsonObject().put("alias", new JsonObject()
   .put("$regex", ".*" + searchKeyword + ".*"
   )
 );


You’re probably mixing up valid queries in the Mongo shell and what’s really valid as a JSON query (no native regex notation in JSON).

Michel

-- 
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/ccf260fe-1a98-4342-ac60-8056455284b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nguyễn Thanh Hải

unread,
Sep 7, 2015, 2:14:25 PM9/7/15
to vert.x
Thanks for the answer, I tried your solution and it worked ! ;) 
So I understand i should build the json query in the mongo shell first, right ?

--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/CzsZx7xlW9Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.

Michel Guillet

unread,
Sep 7, 2015, 3:05:26 PM9/7/15
to ve...@googlegroups.com
It’s just that there is a gap between what you can write in mongo shell and valid JSON query…

Here is a mongo documentation where it’s discussed:

Michel

Nguyễn Thanh Hải

unread,
Sep 8, 2015, 12:14:43 AM9/8/15
to vert.x
:DThanks Michel.  I tried to put this in mongo shell and it also works 

db.deals.find({"alias": { "$regex": ".*lau nuong.*"}})


Reply all
Reply to author
Forward
0 new messages