Question - Lab - Shard a Collection - just curious which db is the index created on?

Hi,

So while doing this lab, I forgot to do a use and I have gotten an error when doing db.adminCommand( { shardCollection: “m103.products”

vagrant@m103:/var/mongodb/db$ mongo --port 26000 -u “m103-admin”
MongoDB shell version v3.6.12
Enter password:
connecting to: mongodb://127.0.0.1:26000/?gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“1402d66b-8ed6-4c78-a970-ee5ac944b153”) }
MongoDB server version: 3.6.12
MongoDB Enterprise mongos> sh.enableSharding(“m103”)

    "ok" : 1,
    "operationTime" : Timestamp(1556375645, 5),
    "$clusterTime" : {
            "clusterTime" : Timestamp(1556375645, 5),
            "signature" : {
                    "hash" : BinData(0,"92NUHQH1bekWwWXD1Q8E4jWa41s="),
                    "keyId" : NumberLong("6684450558465540098")
            }
    }

}

  • Without doing use , I did

MongoDB Enterprise mongos> db.products.createIndex({"": 1})

    "raw" : {
            "m103-repl/192.168.103.100:27001,192.168.103.100:27003,m103:27002" : {
                    "createdCollectionAutomatically" : true,
                    "numIndexesBefore" : 1,
                    "numIndexesAfter" : 2,
                    "ok" : 1
            }
    },
    "ok" : 1,
    "operationTime" : Timestamp(1556375670, 1),
    "$clusterTime" : {
            "clusterTime" : Timestamp(1556375670, 1),
            "signature" : {
                    "hash" : BinData(0,"9JLZ1nZ0oMxPyNWErV1GJ/C/U80="),
                    "keyId" : NumberLong("6684450558465540098")
            }
    }

}

  • Running db.adminCommand( { shardCollection: “m103.products” gives error below:
    “errmsg” : “Please create an index that starts with the proposed shard key before sharding the collection”,

  • So I have to do use m103 and do the createindex again.

  • Question is, how come the create index when I didn’t do a use m103 prior didn’t fail?

  • From below, I have 3 databases that has the products collection. I can see the first createindex created the index on the repl but on which products collection did it do it? On which db? I check the default db which is test and it now also how the products collection, so this is where it was created? BTW, is there a dropindex?

vagrant@m103:/var/mongodb/db$
vagrant@m103:/var/mongodb/db$ mongo --port 27001 -u “m103-admin”
MongoDB shell version v3.6.12
Enter password:
connecting to: mongodb://127.0.0.1:27001/?gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“e2921c88-9828-46b5-9d36-7fdcfa625362”) }
MongoDB server version: 3.6.12
Server has startup warnings:
2019-04-27T10:58:13.855+0000 I STORAGE [initandlisten]
2019-04-27T10:58:13.855+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-04-27T10:58:13.855+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-04-27T10:58:16.485+0000 I CONTROL [initandlisten]
2019-04-27T10:58:16.485+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.
2019-04-27T10:58:16.485+0000 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2019-04-27T10:58:16.485+0000 I CONTROL [initandlisten]
2019-04-27T10:58:16.485+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.
2019-04-27T10:58:16.485+0000 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2019-04-27T10:58:16.485+0000 I CONTROL [initandlisten]
MongoDB Enterprise m103-repl:PRIMARY> show dbs
admin 0.000GB
applicationData 0.030GB
config 0.000GB
local 0.072GB
m103 0.030GB
test 0.000GB
testDatabase 0.000GB
MongoDB Enterprise m103-repl:PRIMARY> use applicationData
switched to db applicationData
MongoDB Enterprise m103-repl:PRIMARY> show collections
products
MongoDB Enterprise m103-repl:PRIMARY> use m103
switched to db m103
MongoDB Enterprise m103-repl:PRIMARY> show collections
products
MongoDB Enterprise m103-repl:PRIMARY> exit
bye
vagrant@m103:/var/mongodb/db$ mongo --port 27004 -u “m103-admin”
MongoDB shell version v3.6.12
Enter password:
connecting to: mongodb://127.0.0.1:27004/?gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“4a0c2893-1696-47a4-a10c-196620c3b0eb”) }
MongoDB server version: 3.6.12
Server has startup warnings:
2019-04-27T12:55:02.288+0000 I STORAGE [initandlisten]
2019-04-27T12:55:02.288+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-04-27T12:55:02.288+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-04-27T12:55:05.112+0000 I CONTROL [initandlisten]
2019-04-27T12:55:05.112+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.
2019-04-27T12:55:05.112+0000 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2019-04-27T12:55:05.112+0000 I CONTROL [initandlisten]
2019-04-27T12:55:05.112+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.
2019-04-27T12:55:05.112+0000 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2019-04-27T12:55:05.113+0000 I CONTROL [initandlisten]
MongoDB Enterprise m103-repl-2:PRIMARY> use admin
switched to db admin
MongoDB Enterprise m103-repl-2:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.065GB
m103 0.042GB
MongoDB Enterprise m103-repl-2:PRIMARY> exit
bye

Yes if you don’t use db it will go to test

You can drop that index

MongoDB Enterprise > db.people.getIndexes()
It will give your index_name
Using that in drop command
db.people.dropIndex(“xyz”)