How can I add a two column unique id to the mongodb in a meteor app?

后端 未结 4 1075
滥情空心
滥情空心 2020-12-08 03:15

I am trying to create a two column unique index on the underlying mongodb in a meteor app and having trouble. I can\'t find anything in the meteor docs. I have tried from

4条回答
  •  既然无缘
    2020-12-08 03:32

    Collection._ensureIndex(index, options)

    Searching inside Meteor source code, I found a bind to ensureIndex called _ensureIndex. For single-key basic indexes you can follow the example of packages/accounts-base/accounts_server.js that forces unique usernames on Meteor:

    Meteor.users._ensureIndex('username', {unique: 1, sparse: 1});
    

    For multi-key "compound" indexes:

    Collection._ensureIndex({first_id:1, another_id:1}, {unique: 1});
    

    The previous code, when placed on the server side, ensures that indexes are set.

    Warning

    Notice _ensureIndex implementation warning:

    We'll actually design an index API later. For now, we just pass through to Mongo's, but make it synchronous.

提交回复
热议问题