Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the chrome console. I have tried from term and even tried to point mongod at the /db/ dir inside .meteor . I have tried

Collection.ensureIndex({first_id: 1, another_id: 1}, {unique: true}); variations.

I want to be able to prevent duplicate entries on a meteor app mongo collection.

Wondering if anyone has figured this out?

I answered my own question, what a noob.

I figured it out.

  1. Start meteor server

  2. Open 2nd terminal and type meteor mongo

Then create your index...for example I did these for records of thumbsup and thumbsdown type system.

db.thumbsup.ensureIndex({item_id: 1, user_id: 1}, {unique: true})
db.thumbsdown.ensureIndex({item_id: 1, user_id: 1}, {unique: true})

Now, just gotta figure out a bootstrap install setup that creates these when pushed to prod instead of manually.

like image 458
Steeve Cannon Avatar asked Apr 16 '12 01:04

Steeve Cannon


4 Answers

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.

like image 162
zVictor Avatar answered Nov 16 '22 21:11

zVictor


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.

like image 28
danny Avatar answered Nov 16 '22 19:11

danny


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.

like image 3
macrozone Avatar answered Nov 16 '22 21:11

macrozone


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});
        }
     ...
like image 1
limeyd Avatar answered Nov 16 '22 19:11

limeyd