Db adminCommand

@Ramachandra_37567 at the end of this project i’m trying to run db.adminCommand( { shardCollection: “m103.products”, key: { <shard_key>: 1 } } ) . and it comes up with this:

MongoDB Enterprise mongos> db.adminCommand( { shardCollection: “m103.products”, key: { <shard_key>: 1 } } )
2019-07-07T22:06:21.370+0000 E QUERY [thread1] SyntaxError: invalid property id @(shell):1:60
MongoDB Enterprise mongos>

You have to replace <shard_key> with the column on which you created index
Please check your steps

How do i connect my primary to mongos? I think once i do this i’ll get a the shard key

sh.enableSharding(“m103”)

2019-07-08T03:47:36.182+0000 E QUERY [thread1] Error: not connected to a mongos :

sh._checkMongos@src/mongo/shell/utils_sh.js:8:15

sh._adminCommand@src/mongo/shell/utils_sh.js:18:9

sh.enableSharding@src/mongo/shell/utils_sh.js:98:12

@(shell):1:1

MongoDB Enterprise m103-repl:PRIMARY>

mongo --port 26000 -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin”

Once you run above you should be at mongos prompt
Please check sh.status() then proceed

Thank you. i was able to get on and create db.products.createIndex({"<shard_key>": 1})

when i try to do the next part db.adminCommand( { shardCollection: “m103.products”, key: { <shard_key>: 1 } } ) i get this error

MongoDB Enterprise mongos> db.adminCommand( { shardCollection: “m103.products”, key: { <shard_key>: 1 } } )
2019-07-08T04:10:38.595+0000 E QUERY [thread1] SyntaxError: invalid property id @(shell):1:60
MongoDB Enterprise mongos> db.adminCommand( { shardCollection: “m103.products”, key: { 6710910241363460115: 1 } } )
{
“ok” : 0,
“errmsg” : “Please create an index that starts with the proposed shard key before sharding the collection”,
“code” : 72,
“codeName” : “InvalidOptions”,
“operationTime” : Timestamp(1562559087, 6),
“$clusterTime” : {
“clusterTime” : Timestamp(1562559087, 6),
“signature” : {
“hash” : BinData(0,“mt+Nmqmx2FDO0d3QMROKXlm9s20=”),
“keyId” : NumberLong(“6710910241363460115”)

The actual command i’m running is MongoDB Enterprise mongos> db.adminCommand( { shardCollection: “m103.products”, key: { 6710910241363460115: 1 } } )

Hi @James_68753,

Please share the output of the following command:

sh.shardCollection(“m103.products”, {6710910241363460115: 1});

Thanks,
Sonali

@Sonali_Mamgain

MongoDB Enterprise mongos> sh.shardCollection(“m103.products”, {6710910241363460115: 1});
2019-07-08T14:20:57.628+0000 E QUERY    [thread1] SyntaxError: illegal character @(shell):1:19
MongoDB Enterprise mongos>

Hi @James_68753,

Please check the syntax error using the following doc:

https://docs.mongodb.com/manual/reference/method/sh.shardCollection/#examples

Also, check if you have 6710910241363460115 as key in the documents.

Thanks,
Sonali

I’m not sure what you mean.

MongoDB Enterprise mongos> sh.enableSharding(“m103”)
{
“ok” : 1,
“operationTime” : Timestamp(1562598641, 4),
“$clusterTime” : {
“clusterTime” : Timestamp(1562598641, 4),
“signature” : {
“hash” : BinData(0,“gzGE4iB4QcnY+WhDn34VPfZgfG8=”),
“keyId” : NumberLong(“6710910241363460115”)
}
}
}
MongoDB Enterprise mongos>

This shows that my key is (NumberLong(“6710910241363460115”) so i’m adding that with the command after that.

}

MongoDB Enterprise mongos> db.adminCommand( { shardCollection: “m103.products”, key: { m103-repl/192.168.103.100:27011} } )

2019-07-08T15:49:57.549+0000 E QUERY    [thread1] SyntaxError: missing : after property id @(shell):1:64
MongoDB Enterprise mongos> 

Here is my added shard:

MongoDB Enterprise mongos> sh.addShard(“m103-repl/192.168.103.100:27011”)
{
“shardAdded” : “m103-repl”,
“ok” : 1,
“operationTime” : Timestamp(1562600781, 1),
“$clusterTime” : {
“clusterTime” : Timestamp(1562600781, 1),
“signature” : {
“hash” : BinData(0,“OM/bJpLAJe5BT8Ui7YWAP4UtUSY=”),
“keyId” : NumberLong(“6710910241363460115”)

What am i doing or typing wrong?

Like the error message indicates, more or less, you JSON document is not legal. after key: you need a simple value or a valid object.

You have to replace <shard_key> with the column on which you created index
Please check your steps

Hi @James_68753,

I have read all of your posts and I am going to list down all the commands that you are trying to execute and we are gonna walk through each of them one by one to analyse the source of error :slight_smile:

db.products.createIndex({"<shard_key>": 1})

db.adminCommand( { shardCollection: “m103.products”, key: { <shard_key>: 1 } } )

db.adminCommand( { shardCollection: “m103.products”, key: { 6710910241363460115: 1 } } )

So before you create a shard key on any collection, you first have to create an index on that field which you intend to use as a shard key.

In the Lab - Shard a Collection, we are using the products collections. we have several fields present in this collection as shown in the screenshot below.

Now let’s say we want to use the salePrice field as our shard key. For this we have to first create index on this field and then we will use this field as a shard key.

db.products.createIndex({“salePrice”: 1})

After creating the index on the salePrice field, we can go ahead and use this field (i.e. salePrice) as a shard key.

db.adminCommand( { shardCollection: “m103.products”, key: { “salePrice”: 1 } } )

Now let’s analyse your queries


db.products.createIndex({"<shard_key>": 1})

In this query, you have created index on the field <shard_key>, which does not exist in any document in our collection. The reason why you were able to create index on this field is because even though there is no document in the collection which contains this field, monogdb will go ahead and create an empty index. For more information refer our documentation.


db.adminCommand( { shardCollection: “m103.products”, key: { <shard_key>: 1 } } )

Now you must have learnt in the course that, each document in the collection must contain the shard key in it. In the above command you have chosen the field <shard_key> as your shard key which clearly do not exist in any of the documents in our collection. So you get an error on executing the command. For more information on this refer our documentation on shard key.


db.adminCommand( { shardCollection: “m103.products”, key: { 6710910241363460115: 1 } } )

In this command, the value that you are using 6710910241363460115 as the shard key is incorrect. I believe you mistook the “KeyId” in the above screenshot as a shard key.

If you still have any doubt, feel free to get back to us. Happy Learning !

Thanks,
Shubham Ranjan
Curriculum Support Engineer