3

The document of Meteor says that it use STRING as the _id by default. What's the meaning behind it? What if I write a script to import a huge csv file but would like to give each of them a random string, what can I do?

waitingkuo
  • 89,478
  • 28
  • 112
  • 118
  • It just means that the IDs will be random strings by default. Your script can just insert each record, and they will have random string IDs automatically generated. – sbking Apr 16 '14 at 09:16
  • But my script is written in python. And it doesn't support to insert a document with random _id by default. Need I implement it by my self? – waitingkuo Apr 16 '14 at 09:18
  • 1
    In that case the records should have mongo generated ObjectIDs and you can specify to use `MONGO` generation in your JavaScript. – sbking Apr 16 '14 at 09:20
  • Thank you for the advice. Seems it's better that I set it as a MONGO-style _id. But I'm still curious about why they use the random string by default – waitingkuo Apr 16 '14 at 09:22
  • 1
    I believe it's mainly for convenience. Strings can be more easily used directly in URLs and HTML without having to call `new Meteor.Collection.ObjectID()` or `doc._id.toHexString()`. Also, strings can be compared directly with the `===` operator rather than using `EJSON.equals()`. Note that Meteor's timestamps in its `ObjectID`s are not useful, so if you want timestamps you should add them in your deny rules or use [a package like `collection-hooks`](https://atmospherejs.com/package/collection-hooks) – sbking Apr 16 '14 at 10:35

1 Answers1

6

There's been quite a bit of discussion about why Meteor uses string IDs instead of ObjectIDs, but it basically boils down to the fact that generating ObjectIDs is a lot harder on the client.

The string id generation is implemented as Random.id() in Meteor. So if you're importing the CSV in Javascript in a Meteor process, just use that.

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224