1

I can't remove the _id index, why?

When I try running the dropIndexes command, it removes all indexes but not the _id index.

Doing 'db.runCommand' doesn't work either:

> db.runCommand({dropIndexes:'fs_files',index:{_id:1}})
{ "nIndexesWas" : 2, "errmsg" : "may not delete _id index", "ok" : 0 }

not ok.

Can i use a field including _id in a composite index?

I couldn't find anything online, the ensureindex command can't do it.

db.fs_files.ensureIndex({'_id':1, 'created':1});

the above command just created a new composite index. i haven't found some similar 'create Index' command.

the default _id index is a unique index?

the getIndexes returns it's not a unique index.

{
     "v" : 1,
     "key" : {
             "_id" : 1
     },
     "ns" : "gridfs.fs_files",
     "name" : "_id_"
 },
 {
     "v" : 1,
     "key" : {
             "created" : 1
     },
     "unique" : true,
     "ns" : "gridfs.fs_files",
     "name" : "created_1"
 }
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
springchun
  • 193
  • 3
  • 13
  • As Tyler said you can't drop an index _id. But you can create a compound index which includes _id. - And yes _id is the default unique index. – Gianfranco P. May 09 '13 at 18:05

2 Answers2

4

There is a createIndex command in addition to ensureIndex also.

E.g.

db.<coll>.createIndex({foo:1})
axel22
  • 32,045
  • 9
  • 125
  • 137
  • so,i cannot create the compound index for the _id index? – springchun Jan 05 '12 at 05:34
  • You can create compound indexes with _id. `createIndex()` is actually called inside `ensureIndex()` to add the index to the collection. The difference is that `ensureIndex()` calls `getLastError` to make sure there was no error during the index creation. So it's best to use `ensureIndex()` – Gianfranco P. May 09 '13 at 18:29
3

You cannot delete the index on "_id" in mongodb.

Please see the documentation here

Tyler Brock
  • 29,626
  • 15
  • 79
  • 79