1

I am trying to create a unique index in mongoose for a field ("event_key"), and I want mongodb to not save if I try to create a duplicate entry. I looked at the docs, and it seems all I need to do is set index: {unique: true} in the schema, but I can't seem to get it to work. I've tried several different permutations and still can't get it to work.

In addition, required: true doesn't seem to be working too since I can save an entry even if I do not pass in an event_key. I'm probably missing something really stupid, and wondering if anyone can help?

Schema

var WistiaAnalyticSchema = new Schema({
  event_key: {type: String, required: true, index: {unique: true}},
  visitor_key: String,
  created: {type: Date, default: Date.now},
  ip: String,
})

Trying to add to database

WistiaAnalytic.create({event_key: '1402230270487e0.2668362990953028'}, function(err) {});
WistiaAnalytic.create({event_key: '1402229819163e0.4385743956081569'}, function(err) {});
WistiaAnalytic.create({ip: '1402229819163e0.4385743956081569'}, function(err) {});
WistiaAnalytic.create({event_key: '1402229819163e0.4385743956081569'}, function(err) {
  console.log(err)
});
Dan Tang
  • 1,273
  • 2
  • 20
  • 35
  • Mongoose creates indexes in the background, so if you're doing this all in one go it may not work as expected as you end up adding the docs before the index has had a chance to be created. – JohnnyHK Jun 08 '14 at 14:06
  • @JohnnyHK Thank you for your comment! Would you know of a workaround then? – Dan Tang Jun 08 '14 at 14:56
  • adding to @JohnnyHK, to add delay instead you need to run all these commands one after another. For this you can use async.js. CHeckout this ink for more info http://stackoverflow.com/questions/17181248/making-mongoose-js-queries-run-synchronously/17296329 – Harpreet Singh Jun 08 '14 at 17:09

1 Answers1

5

Mongoose creates indexes in the background, so you need to delay your create calls until index creation has completed. One way to do that is with the 'index' event of the model:

WistiaAnalytic.on('index', function(err) {
    WistiaAnalytic.create({event_key: '1402230270487e0.2668362990953028'}, function(err) {});
    WistiaAnalytic.create({event_key: '1402229819163e0.4385743956081569'}, function(err) {});
    WistiaAnalytic.create({ip: '1402229819163e0.4385743956081569'}, function(err) {});
    WistiaAnalytic.create({event_key: '1402229819163e0.4385743956081569'}, function(err) {
      console.log(err)
    });
});
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • Thank you. I tried the code, and it doesn't create any entries even after waiting for some time. I'm wondering if there is a problem with the way I set up the indexes? Thank you so much for your help! – Dan Tang Jun 08 '14 at 16:13
  • @DanTang Hmm...it worked fine when I tried it using Mongoose 3.8.12. – JohnnyHK Jun 09 '14 at 02:09
  • I figured my mistake, I had accidentally duplicated the field event_key, and your code works now. Thank you so much! – Dan Tang Jun 09 '14 at 02:48