0

I get this error on updating a record, instance.isNew is set to false before I call save()

errmsg: 'E11000 duplicate key error collection: mydb.mycollection index: date_1 dup key: { : new Date(1552176000000) }'

If I am updating my does mongo care if a key value is equal to what it was before the save is called?

thanks!

3
  • 2
    Makes a lot of sense to me. You have a "unique" index on a field called date and you are inserting exactly the same value as an existing document for that field. You cannot do that, and you probably do not need a "unique" index on that field. Commented Mar 8, 2019 at 0:14
  • You also largely appear confused here as anything you instantiate with var instance = new MyModel() is new. The isNew is simply a flag, but your new MyModel() returns something with a completely separate _id. Therefore it's an "insert" and not an "update". Commented Mar 8, 2019 at 0:37
  • date: { type: Date, unique: true, required: true }, indeed you are right. Hazy day. Thanks @NeilLunn Commented Mar 8, 2019 at 1:06

1 Answer 1

1

From the provide the error on updating the record we can conclude lots of sense as from the provided comments by @Neil Lunn, Please check the below information from your error:

errmsg: 'E11000 duplicate key error collection: mydb.mycollection index: date_1 dup key: { : new Date(1552176000000) }'
  1. Your database name: mydb
  2. Your collection name : mycollection
  3. A unique index on your collection mycollection is : date and it is created as:

    use mydb

    db.mycollection.createIndex({date:1}, {unique:true})

  4. Since there is unique index on date fields you can't put the same object again and again.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.