On a local MongoDB
instance i can connect using the mongo
client, define an index using this command
db.collection.createIndex( { score: 1 } )
and then view that index and the default _id index using this command
db.collection.getIndexes()
If I connect to a Replica Set and run the same commands to create and view the indexes, all that is shown is an empty array
> db.collection.getIndexes()
[ ]
I know the indexes exist, as they are referenced if I use . explain() on a query.
So,how do i view the indexes when using a mongo Replica Set
?
MongoDB shell version: 2.6.10
Mongo DB version
rs0:PRIMARY> db.version()
3.2.17
My local version of mongo was 2.6.10, and after upgrading the local version to 3.x I can now see the indexes in the remote replica set.
Best Answer
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.Create an Index
To create an index, use
db.collection.createIndex()
The db.collection.createIndex() method only creates an index if an index of the same specification does not already exist.
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.
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