1
votes

I was storing data received from an API in Mongodb, and just realized the data was containing an _id, so that, while doing the insert, Mongodb did not create an objectid but inserted the string _id I receive.

So now, I want to convert from string _id to objectid. What is the best strategy to do so?

  1. new_oid = ObjectId(string_id) - timestamp is maintained but machine id & pid coded in the string_id will not be consistent with my server & pid that store the data
  2. new_oid = ObjectId() - timestamp is lost, but machine id & pid coded in new_oid will be consistent with the server that actually stored the data

Thanks!

1
No idea what you are talking about. What is the "string" you are receiving? Is it representative of an ObjectId value or not?Neil Lunn
Yes, The string _id I receive -for example 544c89e1d73ee8330a0002ed- is representative of an ObjectId.Antoine Brunel
@antoinet: You might want to have a look at stackoverflow.com/questions/26624116/…. With a few adaptions, this should help you.Markus W Mahlberg
@NeilLunn I was just wondering how to maintain the timestamp & update the machine & pid to match with my app. Btw, having a lot of reputation does not give you right to say that what I say reeks.Antoine Brunel
@MarkusWMahlberg Interesting but I don't see how to modify machine & pid while leaving the timestamp untouched in your question.Antoine Brunel

1 Answers

1
votes

new_oid = ObjectId(string_id) is fine. It doesn't matter if machine id and process id do match.

While it's possible to extract some information from ObjectIds, I wouldn't rely on it in production code.


About timestamps

The timestamp isn't really a business value, it's the time when the document was inserted into the database, whereas you want to know for example the time an order was received.

Besides that ObjectId doesn't guarantee good timestamps:

The relationship between the order of ObjectId values and generation time is not strict within a single second. If multiple systems, or multiple processes or threads on a single system generate values, within a single second; ObjectId values do not represent a strict insertion order. Clock skew between clients can also result in non-strict ordering even for values, because client drivers generate ObjectId values, not the mongod process.

Therefore I use the timestamp property of ObjectId sometimes during development as a convenient method to find the lastest documents, but don't really care about the content of it.