Move a document from one shard to another shard in other cluster


For a database, having User collection, I want to send a user or one document from collection present in NA Zone Cluster 1 → Shard 1 to EU Zone Cluster 2 → Shard N + 1. Is it possible to do this. Also, is it possible to do this in NodeJS project (App Server).

I have to cover 3 zones in total and my plan is to deploy cluster in each zone having one shard each, in which shard will have a PSS architecture replica set . There is also a chance to migrate a user from one zone to another permanently then for that I would require above functionality.

I have started learning about clusters and sharding recently and I would greatly appreciate any help / suggestions.

Hi @Sasha_N_A and welcome in the MongoDB Community :muscle: !

I think this doc answers exactly your question:

Let me know if you need additional clarifications.


Hi @MaBeuLux88

Thanks a lot, provided link is exactly what I was looking for I just have 2 more questions.

  1. It was mentioned “You must be on a mongos. Do not issue the operation directly on the shard.” in the reference link does this mean I cannot do this on my node js app server?.

  2. Let say my User A (mongo Id: 12345) made 50 comments in NA zone and each document in 50 comment has owner field linked with User A (owner: 12345). Now if I move User A to EU would the comments document get corrupted or would the reference would still work and it will fetch user from EU but comments from NA?

  1. Of course you can. When you are using a sharded cluster, the drivers connect to the mongos. You only connect directly to the shards when you are doing admin operations, upgrades, etc. Check out the chapter 3 of M103 for a better understand of sharded clusters.

  2. I’m going to assume that both the users and the comments comments are sharded collections in your example. This $lookup isn’t possible with MongoDB 5.0.X as the “from” collection (comments collection in this case) can’t be shared as mentioned in the 5.0 $lookup doc. But you are in luck because since 5.1, the “from” collection can now be sharded. See the 6.0 doc. I’d still be very careful with the distribution (shard keys) to make sure the queries aren’t doing any “scatter gather” operations. I’d also start the debate whether or not it’s a good idea to have a separate comments collection. Maybe it could be directly embedded in the articles collection (or items, I don’t know what is being commented). Double check the data modeling to see if there isn’t a better alternative.


1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.