MongoEngine/mongoengine

OperationError: Query for sharded findAndModify must contain the shard key

tzongw opened this issue · 0 comments

tzongw commented
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.