Default _id Index
As per MongoDB BOL Here MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents clients from inserting two documents with the same value for the _id
field. You cannot drop this index on the _id field.
Note: In sharded clusters, if you do not use the _id field as the shard key, then your application must ensure the uniqueness of the
values in the _id field to prevent errors. This is most-often done by
using a standard auto-generated ObjectId.
Create an Index
To create an index, use db.collection.createIndex()
db.collection.createIndex( <key and index type specification>, <options> )
The db.collection.createIndex() method only creates an index if an index of the same specification does not already exist.
Note: MongoDB indexes use a B-tree data structure.
> use StackExchange
switched to db StackExchange
> show collections
position
test
> db.createCollection("score")
{ "ok" : 1 }
> db.score.createIndex( { score: 1 } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.score.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "StackExchange.score"
},
{
"v" : 2,
"key" : {
"score" : 1
},
"name" : "score_1",
"ns" : "StackExchange.score"
}
]
Build Indexes on Replica Sets
For replica sets, secondaries will begin building indexes after the primary finishes building the index. In sharded clusters, the mongos will send createIndex() to the primary members of the replica set for each shard, which then replicate to the secondaries after the primary finishes building the index.
To minimize the impact of building an index on your replica set, you can use the following procedure to build indexes.
Important : To create unique indexes using the following procedure, you must stop all writes to the collection during this
procedure.
If you cannot stop all writes to the collection during this procedure,
do not use the procedure on this page. Instead, build your unique
index on the collection by:
issuing db.collection.createIndex()
on the primary for a replica set,
or
issuing db.collection.createIndex()
on the mongos for a sharded cluster
.
Ensure that your oplog is large enough to permit the indexing or re-indexing operation to complete without falling too far behind to catch up. See the oplog sizing documentation for additional information.
This procedure does take one member out of the replica set at a time. However, this procedure will only affect one member of the set at a time rather than all secondaries at the same time.
Before version 2.6 Background index creation operations become foreground indexing operations on secondary members of replica sets. After 2.6, background index builds replicate as background index builds on the secondaries.
For your further ref Here and Here
mongo
shell withversion()
anddb.version()
? One likely possibility is that you are using an older version of the shell (2.6 or earlier) with a 3.0 or later MongoDB server running WiredTiger. Older clients are not fully aware of newer server versions and features.