4

I am using mongodb with meteor.js
To make multiple fields simultaneously from a collection be unique I use the following code

Messages._ensureIndex({ "numbers": 1, "messageListId": 1, "userId": 1},{unique: true , sparse: true});

messageListId and userId can be null (optional). When numbers field already exist and I want to insert a new object Mongo gives the following error

Error: MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: meteor.messages.$numbers_1_messageListId_1_userId_1  dup key: { : "09355270761", : null, : null } [409]

for instance I want to insert objects same below

Messages.insert({numbers:123,messageListId:456,userId:1});

Messages.insert({numbers:123});

I give the above mentioned error. How to do this in meteor.js?

I see these links but my issue is not resolved with their answer (updated) :

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

Creating Multifield Indexes in Mongoose / MongoDB

Error while setting up compound index

sparse indexes and null values in mongo

Thanks for your attention.

Community
  • 1
  • 1
Saeed Jalali
  • 416
  • 4
  • 16

0 Answers0