findAndModify and delete with justOne=true must be targeted to a single shard which they check by extracting a shard key from their query and targeting the chunk that owns that key. This is done through ShardKeyPattern::extractShardKeyFromQuery() which requires a simple query that will exactly match some value, e.g. {skey: 5} or {skey; {$eq: 5}}. More complex queries that don't produce an exact match, but can still be targeted to a single shard are currently rejected.
This could be improved by changing findAndModify and delete to target using ChunkManager::getShardIdsForQuery(), which returns the shards that own ranges that overlap with the shard key index bounds generated by the query (after checking for an exact shard key match first), and throwing if more than one shard is targeted. This is already how an update is targeted by its query .
- is depended on by
-
SERVER-78385 Update findAndModify and deleteOne to not use two phase protocol if only one shard is targeted
- Closed
- related to
-
SERVER-61683 Operation findAndModify not possible on hashed sharded collection
- Closed
-
SERVER-80307 Allow findAndModify to update shard key without specifying full shard key in the query
- Closed
-
SERVER-88820 Allow updateOne and replaceOne to target by query instead of extracted shard key
- Open
-
SERVER-78894 Allow update to work with a partial shard key and without the two phase protocol
- Closed