How to make unsharded data always go to a specific shard

125 views
Skip to first unread message

Darshan Shah

unread,
Aug 31, 2015, 8:54:42 AM8/31/15
to mongodb-user
Hi,

How to make unsharded DBs & collections always go to a specific shard?
I have setup tag-aware sharding to make data with specific tag to always go to a specific shard.
However, not all collections are sharded, so unsharded collections are assigned to some shard by MongoDb.

I want to create a new shard which would not have any tagged data, but only the unsharded DBs & collections.

Any ideas / suggestions on how to achieve this?

Thanks.

Chris De Bruyne

unread,
Aug 31, 2015, 2:26:08 PM8/31/15
to mongodb-user
Hi Darshan,

unsharded data goes to the "primary" shard by default.
The Primary shard is the original replica-set from where you start your sharding.
If you already have data in a replica-set, this would be your primary.
Otherwise, this would be the first replica-set that you add to your sharded cluster.

But I don't know if it is possible to completely isolate the unsharded data through tagging.

Darshan Shah

unread,
Aug 31, 2015, 2:55:42 PM8/31/15
to mongodb-user
Hi Chris,

Thanks for explaining that the unsharded data goes into the primary shard which is the first replica-set added to the sharded cluster!

Is there any way to specify the primary shard when setting up the cluster? I could not find any such command in the docs.

So I see there are two work-arounds/ways my requirements can be met:
  1. Do not assign any tags to the first shard - that becomes the shard where all unsharded data goes by default
  2. Assign a dummy tag to unsharded data to make it go to a specific shard

Do you see any other better way to achieve this?

Thanks

Chris De Bruyne

unread,
Aug 31, 2015, 3:08:27 PM8/31/15
to mongod...@googlegroups.com
About specifying the primary shard, you can always use the movePrimary command (http://docs.mongodb.org/master/reference/command/movePrimary/#dbcmd.movePrimary )

about the work-arounds, step 2 is not possible because you can only set a tag on a sharded collection, not on un-sharded collections, it's a bit a contradiction .

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/zfXbNi-TWG8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/e708fa25-6085-4e8f-93dd-72b95369e25a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Darshan Shah

unread,
Aug 31, 2015, 3:29:20 PM8/31/15
to mongodb-user
Yes, but isn't movePrimary after the data has been inserted? Or can I issue that command just after creating and setting up the sharded cluster?

Thanks for pointing out the mistake in work-around #2 - so that leaves us with only #1 and the movePrimary command.

Which approach is better & recommended?

Thanks for the quick response and detailed info.

Darshan Shah

unread,
Sep 1, 2015, 1:39:56 PM9/1/15
to mongodb-user
I am testing this and realized that movePrimary command will need to be run every time a DB is created.

So I went with creating a "global" shard as the first shard that does not have any tags assigned.
In spite of that, new DBs get assigned a different primary shard which is not this first "global" shard.

Any ideas what might be going on or what I might be doing wrong?
How do I confirm that the shard is indeed the first one added?

Thanks!

Chris De Bruyne

unread,
Sep 1, 2015, 2:30:49 PM9/1/15
to mongod...@googlegroups.com
If you start from an empty set-up then is not much you can do besides movePrimary.
Be sure to check the documentation because there are more steps to do after you issued that command.

If you don't have the possibility to start from a first replica-set with all db's already created en then start the sharding, then there is not much you can do.
I have been looking a bit deeper in to this and I can not find a way to set the primary-shard.

Maybe there is a hidden gem someone knows of.

Kind regards
Chris


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/zfXbNi-TWG8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

Chris De Bruyne

unread,
Sep 1, 2015, 2:53:06 PM9/1/15
to mongodb-user
Just came across this post of Asya : http://askasya.com/post/taggedcollectionbalancing

Halfway she describes how you can use tags to make collections go to a single shard.
Maybe this can help you too?



On Tuesday, September 1, 2015 at 8:30:49 PM UTC+2, Chris De Bruyne wrote:
If you start from an empty set-up then is not much you can do besides movePrimary.
Be sure to check the documentation because there are more steps to do after you issued that command.

If you don't have the possibility to start from a first replica-set with all db's already created en then start the sharding, then there is not much you can do.
I have been looking a bit deeper in to this and I can not find a way to set the primary-shard.

Maybe there is a hidden gem someone knows of.

Kind regards
Chris

On 1 September 2015 at 19:39, Darshan Shah <dee...@gmail.com> wrote:
I am testing this and realized that movePrimary command will need to be run every time a DB is created.

So I went with creating a "global" shard as the first shard that does not have any tags assigned.
In spite of that, new DBs get assigned a different primary shard which is not this first "global" shard.

Any ideas what might be going on or what I might be doing wrong?
How do I confirm that the shard is indeed the first one added?

Thanks!

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/zfXbNi-TWG8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user+unsubscribe@googlegroups.com.

To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

Darshan Shah

unread,
Sep 2, 2015, 9:52:24 AM9/2/15
to mongodb-user
Ok, so it seems that we just need to shard the collection and tag it so that as to make it go to only one specific shard to achieve my goal.

Can someone however please shed some light on how & which shard is chosen as Primary because in my testing, the first added shard is not always being assigned as the primary shard for a newly created DB.

Thanks

Asya Kamsky

unread,
Sep 9, 2015, 6:11:05 PM9/9/15
to mongodb-user
Each database has a single primary shard and which one it is generally
rotates. But when a DB is first created, you can move its primary
shard (before creating any collections/data though!). See
http://docs.mongodb.org/manual/reference/command/movePrimary/ and read
the various caveats.
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> http://www.mongodb.org/about/support/.
> ---
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-user...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/38d9fe17-7cfc-4814-b654-26e138a5ed54%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages