[SERVER-70489] Building a unique index before shardCollection shows inconsistent index status Created: 12/Oct/22  Updated: 05/Dec/22  Resolved: 26/Oct/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Chao Yin Assignee: [DO NOT USE] Backlog - Sharding EMEA
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-19714 config.collections should reflect if ... Closed
Assigned Teams:
Sharding EMEA
Operating System: ALL
Steps To Reproduce:

First create a unique index.

sh.enableSharding("testdb")
use testdb
db.testcoll.createIndex({a:1}, {unique:true})
db.testcoll.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_"
        },
        {
                "v" : 2,
                "key" : {
                        "a" : 1
                },
                "name" : "a_1",
                "unique" : true
        }
]

Then shard the collection, but do not specify a unique index.

 sh.shardCollection("testdb.testcoll", {a:1})

Finally, it shows that it is not a unique index in the config.collections table, but when I run getIndexes command, it shows that it is a unique index, and the status is inconsistent.

use config
db.collections.find({_id:"testdb.testcoll"})
{ "_id" : "testdb.testcoll", "lastmodEpoch" : ObjectId("63467fa468689fc229d184b2"), "lastmod" : ISODate("2022-10-12T08:49:40.375Z"), "timestamp" : Timestamp(1665564579, 6), "uuid" : UUID("e6eb4a14-60ee-4c00-acb3-355da36b59eb"), "key" : { "a" : 1 }, "unique" : false, "noBalance" : false }

Participants:

 Description   

For a sharded cluster, first create a unique index, but do not specify the relevant field as a unique index when shardColleciton. Finally, the config.collections table shows that the corresponding index is not a unique index.
Is this expected behavior?
When running the shardCollection command without specifying unique equal to true bug finding that the corresponding index has been built as a unique index, should it return a failure?



 Comments   
Comment by Chao Yin [ 12/Oct/22 ]

First create a unique index.
```
sh.enableSharding("testdb")
use testdb
db.testcoll.createIndex({a:1},{unique:true})
db.testcoll.getIndexes()  // show that the a field is a unique index
```
Then shard the collection, but do not specify a unique index.
```
sh.shardCollection("testdb.testcoll", {a:1} )
```
Finally, it shows that it is not a unique index in the config.collections table, but when I run getIndexes command, it shows that it is a unique index, and the status is inconsistent.
```
use config
db.collections.find({_id:"testdb.testcoll"})

{ "_id" : "testdb.testcoll", "lastmodEpoch" : ObjectId("63467fa468689fc229d184b2"), "lastmod" : ISODate("2022-10-12T08:49:40.375Z"), "timestamp" : Timestamp(1665564579, 6), "uuid" : UUID("e6eb4a14-60ee-4c00-acb3-355da36b59eb"), "key" :\{ "a" : 1 }

, "unique" : false, "noBalance" : false }

Generated at Thu Feb 08 06:16:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.