1

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.

3
  • 1
    What are your client and server versions of MongoDB as reported in the mongo shell with version() and db.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.
    – Stennie
    Dec 30, 2017 at 20:48
  • @user3188040,As Stennie has already asked, what is MongoDB version in your environment? Dec 31, 2017 at 12:08
  • @Stennie you were on the right track, it was the older client version that was unable to see the indexes in the newer remote server. If you create an answer I'll mark it as correct. Thanks! Jan 10, 2018 at 21:41

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.