How can i find out the chunk id?

for the lab, i need to submit the chunk id with the validation script. but the chunk id i retrieved appears to be the incorrect one. I am getting a “wrong chunk ID, try again…” error when i run the validation script.

can you please tell me how i can solve this?

figured it out. for those who have the same issue, here is the solution:

run db.chunks.find().pretty() & pass in the chunk id similar to the following with the validation script: m103.products-sku_MinKey

i originally ran db.chunks.findOne().pretty() and the ID returned by that command (config.system.sessions-_id_MinKey) did not work.

hth.

Hi @Kanchana_99113,

Thanks for sharing this.

I tried replicating the same but didn’t get the different chunk IDs. Can you please share the complete expression that you are using in your commands as that’ll help me look at it and debug in a better way?

Regards,
Muskan
Curriculum Support Engineer

the following was returned by the findOne() command:
MongoDB Enterprise mongos> db.chunks.findOne()
{
“_id” : “config.system.sessions-_id_MinKey”,
“ns” : “config.system.sessions”,
“min” : {
“_id” : { “$minKey” : 1 }
},
“max” : {
“_id” : { “$maxKey” : 1 }
},
“shard” : “m103-repl”,
“lastmod” : Timestamp(1, 0),
“lastmodEpoch” : ObjectId(“5d8b7a0d690d00ba7e6c3c87”)
}

the following was returned by the find() command:
MongoDB Enterprise mongos> db.chunks.find().pretty()
{
“_id” : “config.system.sessions-_id_MinKey”,
“ns” : “config.system.sessions”,
“min” : {
“_id” : { “$minKey” : 1 }
},
“max” : {
“_id” : { “$maxKey” : 1 }
},
“shard” : “m103-repl”,
“lastmod” : Timestamp(1, 0),
“lastmodEpoch” : ObjectId(“5d8b7a0d690d00ba7e6c3c87”)
}
{
“_id” : “m103.products-sku_MinKey”,
“lastmod” : Timestamp(2, 0),
“lastmodEpoch” : ObjectId(“5d8bda5f690d00ba7e6e3c6a”),
“ns” : “m103.products”,
“min” : {
“sku” : { “$minKey” : 1 }
},
“max” : {
“sku” : 23153496
},
“shard” : “m103-repl”
}
{
“_id” : “m103.products-sku_23153496”,
“lastmod” : Timestamp(2, 1),
“lastmodEpoch” : ObjectId(“5d8bda5f690d00ba7e6e3c6a”),
“ns” : “m103.products”,
“min” : {
“sku” : 23153496
},
“max” : {
“sku” : 28928914
},
“shard” : “m103-repl-2”,
“jumbo” : false
}
{
“_id” : “m103.products-sku_28928914”,
“ns” : “m103.products”,
“min” : {
“sku” : 28928914
},
“max” : {
“sku” : { “$maxKey” : 1 }
},
“shard” : “m103-repl-2”,
“lastmod” : Timestamp(1, 2),
“lastmodEpoch” : ObjectId(“5d8bda5f690d00ba7e6e3c6a”)
}

Hi @Kanchana_99113,

If you see the output of find(), that also includes the entry with the chunk ID that shows up in the output of findOne().
The findOne() method basically returns just the first entry from the list of results.

Moreover, this lab demands to find one particular document whose key value is "sku" : 21572585 and the lab notes also hint (see below snippet) at looking within a range of min/max sku keys to find this document.

hint sh.status() does not provide the chunk ID that you need to report for this lab. Look in the config database for the collection that stores all chunk information. Think in ranges - you want to find the chunk whose range is min <= key < max.

So you should be using an expression in the find() function which will result into the correct answer, i.e. one appropriate result containing the right chunk ID.

Thanks,
Muskan
Curriculum Support Engineer

Hi,

I’m can’t the chunk_id, I do this query:
use config
db.chunks.find({sku: {$gte : 21572585, $lt : 24799312}})
But nothing return
Help me please

Hi @Jessyre,

The chunk that has been asked for in this lab is the one contained in m103.products.

If you simply use this query in chunks to filter out on a range of one of the fileds in products collection, it won’t result into the appropriate and desired answer.

Instead, I would suggest you to first just query the chunks collection as below and analyze it’s structure. Then you may go ahead and use your query to filter out the results and find your desired chunk.

db.chunks.find().pretty()

Let me know if this helps!

Thanks,
Muskan

I only get this:

MongoDB Enterprise mongos> db.chunks.findOne()

null

You’re using the wrong db.

1 Like

Hi @JavierBlanco,

Please read the hint section and you will figure out the db to use.

Please let me know if you have any other questions.

Thanks,
Shubham Ranjan
Curriculum Support Engineer

I noticed that the "id" for a chunk ends with (e.g., sku_12345678). When it literally ends with sku_MinKey, how can I know the value of MinKey? I also noticed that in some cases both “min.sku” and “max.sku” have number values (e.g., 12345678), but in other cases, they have either “$minKey”:1 or “$maxKey”:1 value. How can I make a query? What does “$minKey”:1 or “$maxKey”:1 mean? I assume their values will change, but is there any way to know their current values?

Hi @Masud_20882,

Good question :slight_smile:

Let’s understand the entire structure with an example. Let’s say this is one document in our chunks collection.

{
“_id” : “Something_test_123456789”,
“lastmod” : Timestamp(2, 1),
“lastmodEpoch” : ObjectId(“5d8bda5f690d00ba7e6e3c6a”),
“ns” : “DatabseName.CollectionName”,
“min” : {
“Shard_key_name” : "Some value"
},
“max” : {
“Shard_key_name” : { “$maxKey” : 1 }
},
“shard” : “m103-repl-2”
}

_id : It is the chunk id.

min : The minimum value of the shard key which any document has in this chunk.

max : The maximum value of the shard key which any document has in this chunk.

Now, your real confusion is here : {“$minKey”:1} and { “$maxKey” : 1 }

Here, it does not mean that the minKey and maxKey is 1. Rather it means whatever is the minimum value and the maximum value present in any document in that chunk.

Hope it helps!

If you have any other query then please feel free to get back to us.

Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

Just to add…

I presume that this naming convention was chosen as a means of making the id unique and somewhat meaningful when read.

The values are arbitrary and by BSON convention, minKey and maxKey are typically used to denote the smallest and largest values. Execute a query to find out their values.

And remember that the boundaries of a chunk has an inclusive minimum and an exclusive maximum. For example, in the image below, if the value of X is 20, it’ll be stored in Shard C and not Shard B:

Hi @Masud_20882,

In continuation of the post #13.

You can write a simple query for this or you can also do it using aggregation. Here is a simple query for finding the max value.

db.getSiblingDB("Database Name").Collection_Name.find().sort({"sku" : -1}).limit(1)

Plug in the values.

Hope it helps!

Thanks,
Shubham Ranjan
Curriculum Support Engineer

Thank you, Shubham. I was wondering if there was an arbitrary value for MinKey or MaxKey. It appears that MinKey is a value for $minKey and MaxKey for $maxKey, as I was able to use them in my query (e.g., by checking if min,sku was less than or equal to the target sku OR equal to MinKey) to search for the chunk for a particular document. However, I have learned a new query, as you showed me. You have been very helpful!

Here is an overview of my running mongo/d/s processes=>
vagrant@m103:~ ps -ef |grep mongo vagrant 1481 1 2 01:23 ? 00:11:40 mongod -f csrs_1.conf vagrant 1564 1 2 01:23 ? 00:12:19 mongod -f csrs_2.conf vagrant 1860 1 1 01:26 ? 00:09:36 mongod -f mongod-repl-1.conf vagrant 1938 1 1 01:26 ? 00:09:31 mongod -f mongod-repl-2.conf vagrant 2021 1 1 01:26 ? 00:09:30 mongod -f mongod-repl-3.conf vagrant 2129 1 1 01:27 ? 00:09:23 mongod -f mongod-repl-4.conf vagrant 2160 1 1 01:27 ? 00:09:30 mongod -f mongod-repl-5.conf vagrant 2190 1 1 01:27 ? 00:09:15 mongod -f mongod-repl-6.conf vagrant 2330 1 0 01:30 ? 00:02:01 mongos -f mongos.conf vagrant 11183 1 3 10:13 ? 00:00:42 mongod -f /etc/csrs_3.conf vagrant 11291 8846 0 10:13 pts/4 00:00:00 mongo --port 26000 -u m103-admin -p xxxxxxxxx --authenticationDatabase admin vagrant 11632 10402 0 10:33 pts/1 00:00:00 grep --color=auto mongo vagrant@m103:~

AND to find the chunk id for a given document this is what am trying to catch the range for a given document , it executes but no result ?
OUTPUT =>
MongoDB Enterprise mongos> db.chunks.find({“min”:{sku: {$eq: 21572585}},“max”:{sku: {$ge: 21572585}}})
MongoDB Enterprise mongos>

Two things:

  1. Syntax problem
  2. Check that you’re using the right db

The first thing you’d want to do is look at the structure of this collection (i.e. db.chunks.findOne() or db.chunks.find().limit(3).pretty()), after that you’ll know what fields to use.

By the way, did you take the pre-requisite course M001 Basics? Or the M121 Aggregation Framework course? They both will help with query syntax.

switched to db config
MongoDB Enterprise mongos> db.chunks.find({“min”:{sku:21572585},“max”:{sku: {"$maxKey":9999265900050000}}})
but NO RESULT…not sure what i am missing after finding the range maxkey here?

Same incorrect syntax. Have a look through the documentation for find() to see examples.

Just double checking again, have you taken the prerequisite for this course, M001 Basics?

Hi @sunder1,

Here, you are trying to query a subdocument. Please use the dot notation such as (“field.nestedField”). For more information on this please refer our documentation.

In addition to this, the query predicate would not give the desired result. You might want to do it using a range query predicate.

Hope it helps!

Thanks,
Shubham Ranjan
Curriculum Support Engineer