Open xgamen opened 10 years ago
Ok, I found a solution. I had to convert the string back to normal string (asc) before I use objectid.new( str ) function in the driver. I expected to work from the string I get from objectid.tostring(). But, I had to do conversion first.
first, convert hex string to number: tonumber( hexstr, 16). second, convert that to normal string: string:gsub('..', string.char( number ) )
so, here is the funtion:
function convertHexStringToNormal( str ) return (str:gsub('..', function (cc) return string.char(tonumber(cc, 16)) end)) end
Then, you can do: objid.new( string_from_above ) to get back to 'object' id.
now, you can query using this id for '_id'.
Thanks @xgamen, it helped me a lot! I leave an example of using this in a query: local object_id = require "resty.mongol.object_id" r = col:find_one({_id = object_id.new(convertHexStringToNormal("53c24a247661670d08000000"))})
I am glad that it helped you, krakatoa. Cheers.
Hi, I have a problem with working with object ID.
I like to use object ID to be a unique id for a document. And, when I need to serialize that to string so tht I can send as JSON doc, it works with objectID's tostring() function.
Now, when I receive the string, I tried to convert that to object ID and it asserts. It's because object ID was 12 byte, and when the object ID converted to string (hex string %02x), it becomes 24 bytes. Now, when I receive the data, your library check whether the side is 12 bytes so it won't work.
Even though I removed the assert to see whether it will convert back, it does not.
So, there seem to be no way that I can convert to object ID again from the string so that I can run a query based on that.. I also use PyMongo in my python cron job, but, it works fine in that library.
any thought? or any good idea to work around? I tried many different method, but, i just cannot get back to the obj id.