Related Non MongoIds for meteor

Hi, using Collection2, Autoform, Meteor Admin, some how I lost my thinking cap and I can’t remember how to tell my related Schema of tags to use a the non mongoID but my TagID instead.

@Tags = new Mongo.Collection('tags')

Schemas.Tag = new SimpleSchema(
  tags:
    type: [ Schemas.Tag]
  tagId:
    type: Number
  name:
    type: String
  createdAt:
    type: Date
    autoValue: ->
      if this.isInsert
        new Date()

  updatedAt:
    type: Date
    optional: true
    autoValue: ->
      if @isUpdate
        new Date()
  )
Tags.attachSchema(Schemas.Tag)

If you want to use your own primary key instead of the Mongo pre-generated one, you can just set _id to be whatever you want, i.e.
TestCol.insert({_id: 'yellow', ...});

If you are asking how to tell Mongo to use TagID instead of _id as a primary key, unfortunately you can’t, but Mongo supports secondary indexes and SimpleSchema can enforce uniqueness.
So by creating an index on TagID and setting it to be unique you can simulate it as a primary key if you cannot change the TagID key.

1 Like