MongoClient issue handling ObjectIDs properly

639 views
Skip to first unread message

Thomas Reinecke

unread,
Aug 31, 2015, 10:29:30 AM8/31/15
to vert.x

Hi, I'm trying to find data in my mongodb collection "user" using io.vertx.ext.mongo.MongoClient 

I wanna query for a specific document by its _id. 

JsonObject primaryFilter = new JsonObject();

primaryFilter.put("_id", new org.bson.types.ObjectId.ObjectId("55dc66a5c483983722efb8b1"));


at runtime I'm getting :


java.lang.IllegalStateException: Illegal type in JsonObject: class org.bson.types.ObjectId

at io.vertx.core.json.Json.checkAndCopy(Json.java:115)

at io.vertx.core.json.JsonObject.put(JsonObject.java:591)

at com.ibm.ic.webui.verticle.DataProviderVerticle.handleRequestData(DataProviderVerticle.java:59)

at com.ibm.ic.webui.verticle.DataProviderVerticle$$Lambda$12/1019412361.handle(Unknown Source)

at io.vertx.core.eventbus.impl.EventBusImpl$HandlerRegistration.handle(EventBusImpl.java:1108)

at io.vertx.core.eventbus.impl.EventBusImpl.lambda$doReceive$189(EventBusImpl.java:755)

at io.vertx.core.eventbus.impl.EventBusImpl$$Lambda$92/383514363.handle(Unknown Source)

at io.vertx.core.impl.ContextImpl.lambda$wrapTask$15(ContextImpl.java:314)

at io.vertx.core.impl.ContextImpl$$Lambda$7/79290250.run(Unknown Source)

at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:357)

at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)

at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)

at java.lang.Thread.run(Thread.java:745)


So io.vertx.core.json.JsonObject cant take a org.bson.types.ObjectId to define a mongodb Objectid, thats understood.

How can I then query for a specific document _id ?


the doc in my collection looks like this:


{

    "_id" : ObjectId("55dcd02c158c1d3a5fa423f2"),

    "serial" : "xyz",

    "firstname" : "Hans",

    "lastname" : "Tester",

    "email" : "xyz",

}


I also tried the following:


mongo.find(collection, new JsonObject("{\"_id\":\"55dcd02c158c1d3a5fa423f2\"}, lookup -> {

}


this gives me an empty resultset. it cant really match this string to the objectid in mongodb

Thanks for any help

Thomas Reinecke

unread,
Aug 31, 2015, 10:43:19 AM8/31/15
to vert.x
I just realized by http://vertx.io/docs/vertx-mongo-client/java/#_configuring_the_client that we can set 

useObjectId to true when initializing MongoClient.

However that still doesnt work for me to query by documentid via


mongo.find(collection, new JsonObject("{\"_id\":\"55dcd02c158c1d3a5fa423f2\"}"), lookup -> {


it still doesnt find anything...

any help is highly appreciated



mongoconfig.put("useObjectId", "true");

David Bush

unread,
Aug 31, 2015, 3:22:06 PM8/31/15
to vert.x
Sadly, 3.0.0 doesn't support ObjectId and useObjectId doesn't work. I submitted Pull Requests for both which have been merged into 3.1.0-SNAPSHOT.

If you try that version, it should work for you.

if you set useObjectId to true, you can set the _id like this:

new JsonObject().put("_id", new ObjectId().toHexString());

This will happen automatically on save however if you set useObjectId to true. Just don't set the _id at all.

To set an Object ID on some other attribute (not _id):

new JsonObject().put("someoid", new JsonObject().put("$oid", new ObjectId().toHexString()));

David Bush

unread,
Aug 31, 2015, 3:37:52 PM8/31/15
to vert.x
Actually, to be more clear, to create your filter:

mongoConfig.put("useObjectId", true);



JsonObject primaryFilter = new JsonObject();

primaryFilter
.put("_id", some_previously_saved_hex_string);

This assumes that you used version 3.1.0-SNAPSHOT and saved the document previously with useObjectId = true, or it was saved with an ObjectId through some other means like a Python script or something.

To get the latest version of the mongo client:

git clone https://github.com/vert-x3/vertx-mongo-client.git
cd vertx
-mongo-client
mvn install


That will install 3.1.0-SNAPSHOT to your local maven repository. Then just include that version in your project's POM. It isn't yet throughly tested, so be careful.

Thomas Reinecke

unread,
Sep 1, 2015, 2:55:07 AM9/1/15
to vert.x

You're absolutely right, David. I pulled the latest vertx-mongo-client yesterday as I saw the code changes related to useObjectId 
in the JsonObjectCodec.java and MongoClientImpl.java codes on Github.

With that I could get an _id based find it to work with this code:

JsonObject filter = new JsonObject().put("_id", new JsonObject().put("$oid", "55dcd02c158c1d3a5fa423f2"));

mongo.find(collectionName, filter, lookup -> {

 ...


Thanks David, much appreciated
Reply all
Reply to author
Forward
0 new messages