Extract mongodb object id to a string

I’d like to take the mongodb object id and extract the id itself, instead of getting the object id. The reason is that whenever I try to use the json in the code that builds the seed in my database it won’t cast the id, but when it’s a string instead of an object it works fine.

So, I want this:

  "_id": {
    "$oid": "577d7a19fc13ae11f300aeea"
  }

to be this:

"_id": "577d7a19fc13ae11f300aeea"

So far, I’ve tried using

return this.send(:$oid)

but I don’t have access to ruby methods. And

return this.$oid

but that returned a syntax error.

I’m a ruby novice. How can I just get the string and not the object?

Update: Well, for now I’m just using a regex like this:

(\d|[:lower:]){24}

1 Like

I’ve made it so you can now simply call this.to_s to get the id as a string.

2 Likes