如何在 Meteor 中为嵌入文档设置唯一 ID?

How to set a unique id for an embedded document in Meteor?

我已经使用简单模式设置了我的 collections :

SubLinkSchema = new SimpleSchema({
    name: {
        type: String,
        label: 'Link Name',
        unique: false
    },
    link: {
        type: String,
        regEx: SimpleSchema.RegEx.Url,
        label: 'Custom Link',
        optional: true,
        autoform: {
            class: 'sub-custom-link'
        }
    }

});

LinkSchema = new SimpleSchema({
    name: {
        type: String,
        label: 'Link Name',
        unique: false
    },
    link: {
        type: String,
        regEx: SimpleSchema.RegEx.Url,
        label: 'Custom Link',
        optional: true,
        autoform: {
            class: 'main-custom-link'
        }
    },
    subLinks: {
        optional: true,
        label: 'Sub Links',
        unique: false,
        type: [SubLinkSchema]
    }
});

这里的问题是,子链接没有得到一个ID。没有 id 很难更新它们。那么,如何为每个子链接(嵌入文档)生成唯一 ID?

它应该与

Meteor.uuid()

在 SimpleSchema 中使用自动值字段

请参阅此处参考: https://github.com/aldeed/meteor-collection2#autovalue

和示例:

subLinkID: {
    type: String,
    autoValue: function() {
        return Meteor.uuid();
    }
  }