最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor Collection Document IDs: Random.id() or Meteor.Collection.ObjectID() - Stack Overflow

programmeradmin1浏览0评论

When I insert documents into my Meteor collections, they have an _id with the form of Random.id:

Random.id();
// "wjQyQ6sGjzvNMDLiJ"

When I insert documents into those same collections directly from MongoDB, they have an _id in the form of Meteor.Collection.ObjectID.

new Meteor.Collection.ObjectID();
// LocalCollection._ObjectID {_str: "b105582bc495617542af18e9"…}

Why does my app use Random.id? Is this a legacy setting?

Meteor versions when I created my app:

[email protected]
[email protected]

When I insert documents into my Meteor collections, they have an _id with the form of Random.id:

Random.id();
// "wjQyQ6sGjzvNMDLiJ"

When I insert documents into those same collections directly from MongoDB, they have an _id in the form of Meteor.Collection.ObjectID.

new Meteor.Collection.ObjectID();
// LocalCollection._ObjectID {_str: "b105582bc495617542af18e9"…}

Why does my app use Random.id? Is this a legacy setting?

Meteor versions when I created my app:

[email protected]
[email protected]
Share Improve this question asked Mar 27, 2015 at 14:21 chrischris 7272 gold badges5 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

When creating your Meteor collection programatically from your application, you have the ability to specify an option that determines what type of ID generation method is used to generate new IDs for documents in that collection. By default, a random string generation function is used if no option is specified. Check out the Meteor documentation to see exactly what I am talking about. If you do not specify the option, Meteor simply uses the random package in order to generate these ID strings. If you check the link, you will see that the first item in the list is a random ID generation function. This is where the Random.id() function is being called. Obviously, going directly to MongoDB bypasses this possible flow of logic, resulting in the MongoDB type of ID string.

发布评论

评论列表(0)

  1. 暂无评论