I want to generate the mongoDB's _id from R side before saving data in mongoDB. I'm using package mongolite but there isn't this feature, are there others ways to achieve this goal?
Asked
Active
Viewed 199 times
2
-
Not possible on the client side: https://docs.mongodb.com/manual/reference/method/ObjectId/#ObjectId Why do you need it at all? – Alex Blex Jan 22 '18 at 11:23
-
You can create your own index in this manner along with the _id generated by mongodb: https://jeroen.github.io/mongolite/query-data.html#indexing – tushaR Jan 22 '18 at 11:26
-
@TUSHAr I don't want to do this. I want generate _id client side using R. – Terru_theTerror Jan 22 '18 at 11:30
-
@AlexBlex In this topic https://stackoverflow.com/questions/10593337/is-there-any-way-to-create-mongodb-like-id-strings-without-mongodb this is possible client side. – Terru_theTerror Jan 22 '18 at 11:31
-
just use the `ObjectId()` method to generate and assign to _id. ObjectId is a time based hash string (base16). If you are unable to use the function you can use the following [gist](https://gist.github.com/solenoid/1372386) – imixtron Jan 22 '18 at 11:35
-
3@Terru_theTerror, it is implemented in the driver, not on the client app. You can follow the referred answers to create an id that **looks like** objectid, but I don't see much use of it to be honest. This is why I asked why do you need at the first place. The _id can be anything really. – Alex Blex Jan 22 '18 at 11:49
-
@AlexBlex I do this because I want to have in memory the _id (and use it later) without doing an additional query on the database. – Terru_theTerror Jan 22 '18 at 12:10
-
@imixtron, thank you. Is there the same code already implemented in R? – Terru_theTerror Jan 22 '18 at 12:12
-
@Terru_theTerror, sorry if my question was not clear enough. I understand that there are many valid reasons to assign `_id` from the client app. I was asking why you need it in ObjectId format? There is no special requirement for the `_id` field but to be unique. 1, 2, 3 , 'one', 'two', 'three', '9999c933-60fc-4752-ba39-9e6300a72fff', '588fb589-df5b-460b-ab93-7db8b77a129f' are all valid values for `_id` field as long as they remain unique within a collection. – Alex Blex Jan 22 '18 at 12:16
-
oops, my bad. Somehow I read "mongoDB's _id" in the question as "ObjectId". Just generate any random number, and you will be fine. I often use uuid. – Alex Blex Jan 22 '18 at 12:20
-
Other systems write on the same db and my _id must be coherent and correctly sortable by time with them. – Terru_theTerror Jan 22 '18 at 12:28
-
I dont know for sure, but you can easily convert it into your desired language. – imixtron Jan 22 '18 at 12:31
-
Yes I know, thank you. I'm just lazy ;-) If you write the answer I will assign it to you. – Terru_theTerror Jan 22 '18 at 12:33