Query using mongo-persistor when the _id is a UUID (BinData)

2,382 views
Skip to first unread message

Sasanka Panguluri

unread,
Nov 13, 2014, 2:49:03 PM11/13/14
to ve...@googlegroups.com
Hi,

I have my application storing documents in a collection called files, and it's being done by Spring-Data MongoRepository.
The ID for the repository is a UUID. Looking at the collection in Mongo Shell, it looks like this

{ "_id" : BinData(3,"5BG1VMBz6X4bxdWlAgDtqg=="), "_class" : "com.myfiles.file", "authorizationUrl" : "http://localhost:3000/validate_access_token" }


I have the string representation of this UUID again, in a different module of my project that uses Vertx. I am required to query this collection and get the corresponding document.
It seems that JsonObject doesn't let me use UUID directly. Can someone throw some light at this with some possible workarounds?


Thanks!

Nick Scavelli

unread,
Nov 14, 2014, 7:30:38 PM11/14/14
to ve...@googlegroups.com
You can use extended json http://docs.mongodb.org/manual/reference/mongodb-extended-json/#binary but I don't think the mongo persistor supports this yet. It used to (by accident) in version 2.1.0 I think ?

Sasanka Panguluri

unread,
Nov 17, 2014, 11:16:16 AM11/17/14
to ve...@googlegroups.com
Hey,

Thanks for the reply. Can you walk me through how I can use the extended Json here?
I've posted my code here:

http://stackoverflow.com/questions/26935508/mongodbjava-how-do-i-query-a-stored-uuid-in-byte-format


Thank you!

Nick Scavelli

unread,
Nov 17, 2014, 12:45:29 PM11/17/14
to ve...@googlegroups.com
Extended JSON for BinaryData as stated in mongo documentation is

{ "$binary": "<bindata>", "$type": "<t>" }

So for your id field you pasted originally it would look like

{"_id" : {"$binary", "5BG1VMBz6X4bxdWlAgDtqg==", "$type":"3"}}

You'll still need to convert the UUID manually though.

Sasanka Panguluri

unread,
Nov 17, 2014, 2:26:21 PM11/17/14
to ve...@googlegroups.com
Thanks again! I've tried the same query [ db.files.find({"_id" : {"$binary":"5BG1VMBz6X4bxdWlAgDtqg==", "$type":"3"}} ]in Mongo Shell, and it didn't work. 


The Java Driver's BasicDBObject doesn't work with this either, and so is the case with JsonObject.
The errors say that the $binary and $type operators are invalid. Have I missed something?

Nick Scavelli

unread,
Nov 17, 2014, 4:12:14 PM11/17/14
to ve...@googlegroups.com


On Monday, November 17, 2014 2:26:21 PM UTC-5, Sasanka Panguluri wrote:
Thanks again! I've tried the same query [ db.files.find({"_id" : {"$binary":"5BG1VMBz6X4bxdWlAgDtqg==", "$type":"3"}} ]in Mongo Shell, and it didn't work. 

It won't work in the shell, as the shell doesn't use extended json.
 


The Java Driver's BasicDBObject doesn't work with this either, and so is the case with JsonObject.
The errors say that the $binary and $type operators are invalid. Have I missed something?

What version of mongo-persister are you using ? Like I said in my first post, the latest version (2.1.1) won't support it, but I think (2.1.0) will. Basically it was one of those undocumented features and a change to improve conversion from JsonObject -> DBObject resulted in this no longer being supported.

Also looks like you can do this as well:

{"_id": {"$uuid", "7ee973c0-54b5-11e4-aaed-0002a5d5c51b"}}

which looks a lot easier !. 

You can also do the $binary way as well, but one minor correction to my json before is that $type should be an int.
Reply all
Reply to author
Forward
0 new messages