11

The command db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} ) fails on mongo version 3.4.2, but not 3.2.11. The mongo documentation indicates the version 3.4 supports both the unique and background attributes.

Mongo 3.4.2 fails ...

> use testDB
switched to db testDB
> db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} )
{
    "ok" : 0,
    "errmsg" : "The field 'unique' is not valid for an _id index specification. Specification: { ns: \"testDB.testCollection\", v: 1, key: { _id: 1.0 }, name: \"_id_2\", unique: true, background: true }",
    "code" : 197,
    "codeName" : "InvalidIndexSpecificationOption"
}
> 

Mongo 3.2.11 works ...

> use testDB
switched to db testDB
> db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} )
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 1,
    "note" : "all indexes already exist",
    "ok" : 1
}
> 

Anyone know of a work around?

We're using the Mongoose Node.js wrapper to create the Mongo indexes, so not adding the unique and background attributes isn't an option.

Cheers!

Ed

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
icedawn
  • 111
  • 1
  • 1
  • 7

3 Answers3

12

That unique is not problem here.. It's that _id, what already have index (created automatically) and you cannot create second index what have exactly same fields (_id:1) what first one have.

How about testing with some other field than _id and you will find out that unique and background is possible, as long as that field don't have index already present.

JJussi
  • 1,540
  • 12
  • 12
  • 1
    Thanks J.Jussi, that's it. I didn't grok the note from 3.2.11 which was pointing out the redundant indexing request. So obvious now that you've pointed it out. – icedawn Feb 26 '17 at 20:21
  • @JJussi - what would you suggest for the index { _id: -1 } to support reverse order sorts. Is it not necessary? – UpTheCreek May 10 '18 at 09:54
  • Nope, not necessary because mongodb can use index both ways. – JJussi May 11 '18 at 07:37
4

in mongodb3.4, unique and background are not supported in _id field, other fields are possiable.

左晓良
  • 47
  • 1
-1

This error occurred to me also while I was writing test case using sinon.stub to call the functions. The cause was, I was passing wrong data while stubbing that functions.

code - ( error code )

const accountstub = sinon.stub(accountservice, "getall");

accountstub.callFake(()=> accountmockdata)

resolved

const accountstub = sinon.stub(accountservice, "getall");

accountstub.callFake(()=> [accountmockdata])

Solution - It was expecting an array of objects , and I was passing the object only . If you use index in your DB, Please be sure I you are passing the right .

You can console the data by running on dev environment and then see what kind of data that functions return (object, array, etc ) and paste the same data in your mock file.