MongoEngine / MongoEngine/mongoengine

OperationError: Query for sharded findAndModify must contain the shard key

Open
#2,772 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

class User(DynamicDocument):
    name = StringField()

_id is the shard key

user.modify(name='ok')

gets this error

mongoengine.errors.OperationError: Update failed (Query for sharded findAndModify must contain the shard key, full error: {'ok': 0.0, 'errmsg': 'Query for sharded findAndModify must contain the shard key', 'code': 61, 'codeName': 'ShardKeyNotFound'})

the command log is

Command SON([('findAndModify', 'user'), ('query', {'$and': [{'_id': ObjectId('60d96a58ea9596f92ba54c7e')}, {'_id': ObjectId('60d96a58ea9596f92ba54c7e')}]}), ('new', True), ('update', {'$set': {'name': 'ok'}}), ('upsert', False)])

Environment

  • Python 3.11.2
  • mongodb 4.4.17
  • mongoengine 0.27.0
  • pymongo 4.5.0

when I use older project with same db, it works without error

the command log is

Command SON([('findAndModify', 'user'), ('query', {'_id': ObjectId('60d96a58ea9596f92ba54c7e')}), ('new', True), ('update', {'$set': {'name': 'ok'}}), ('upsert', False)])

Older Environment

  • Python 3.5.3
  • mongodb 4.4.17
  • mongoengine 0.19.0
  • pymongo 3.12.3

I notice newer version query has a $and operator, and I try it in mongo shell, gets same error.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the User.modify(name='ok') entry point and compare the generated findAndModify query with the older MongoEngine and PyMongo command logs. Reproduce the sharded update against MongoDB 4.4.17 and verify that the resulting query contains the shard key without the problematic $and structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.