Update_one on sharded collection using _id

Hello everyone,
from what I understand, if a collection is sharded on a key that is not the _id field, there is no guarantee of unicity for the _id field across different shards (https://docs.mongodb.com/manual/core/sharding-shard-key/#unique-indexes).

Then, I don’t understand this statement about the update_one operator:

  • If you don’t specify upsert: true , you must include an exact match on the _id field or target a single shard (such as by including the shard key in the filter).

I understand why the shard key is needed, in order to ensure the operation really updates 1 single item. But why can it be replaced by the _id?

Thanks !

Hi @Daniele_Tessaro

Welcome to MongoDB community.

To my understanding the update one targets one document. So either you need to specify a shard key or an _id if your application enforce this uniqueness.

If its not unique you might get unexpected behaviour…


Thanks for the response. The thing is, as per my original post, you can’t enforce uniqueness on _id across shards, unless it is the shard key. So I don’t understand.


I think the documentation expects your application is enforcing uniqueness… Otherwise you can expect wierd results … Like not the correct document being upserted