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

后端 未结 4 1062
滥情空心
滥情空心 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:26

    Actually why not use upsert on the server with a Meteor.method and you could also send also track it with a ts: // Server Only

    Meteor.methods({
     add_only_once = function(id1,id2){
       SomeCollection.update(
         {first_id:id1,another_id:id2},{$set:{ts:Date.now()}},{upsert:True});
     }
    });
    

    // Client

    Meteor.call('add_only_once',doc1._id, doc2._id);
    

    // actual code running on server

    if(Meteor.is_server) {
        Meteor.methods({
            register_code: function (key,monitor) {
                 Codes.update({key:key},{$set:{ts:Date.now()}},{upsert:true});
            }
         ...
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 03:32

    The Smartpackage aldeed:collection2 supports unique indices, as well as schema-validation. Validation will both occure on server and client (reactivly), so you can react on errors on the client.

    0 讨论(0)
  • 2020-12-08 03:41

    According to the docs "Minimongo currently doesn't have indexes. This will come soon." And looking at the methods available on a Collection, there's no ensureIndex.

    You can run meteor mongo for a mongo shell and enable the indexes server-side, but the Collection object still won't know about them. So the app will let you add multiple instances to the Collection cache, while on the server-side the additional inserts will fail silently (errors get written to the output). When you do a hard page refresh, the app will re-sync with server

    So your best bet for now is probably to do something like:

    var count = MyCollection.find({first_id: 'foo', another_id: 'bar'}).count()
    if (count === 0)
        MyCollection.insert({first_id: 'foo', another_id: 'bar'});
    

    Which is obviously not ideal, but works ok. You could also enable indexing in mongodb on the server, so even in the case of a race condition you won't actually get duplicate records.

    0 讨论(0)
提交回复
热议问题