Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Handle Shard keys with save() #388

rozza opened this Issue · 5 comments

3 participants


@crittercism have used _meta to set shard key data and patched save() so update works across shards.

Patch: crittercism/mongoengine@b635a97


I'll prepare the pull request later today or this weekend as I'm at MongoSV today.

Also, I need to write tests for the patch, and maybe there is a way to introspect the shard key from the database.


I'm not sure if it was a bug with _delta() or not but we also had to add some code to remove the shard keys from set_data inside _delta. We weren't changing the shard key fields but for whatever reason they still ended up in set_data.

Mongo throws an error if you issue an update that includes a $set operation on shard keys (even if the shard key values passed into the criteria are equivalent to the $set values)


I think the expected behavior for save is:

  1. Send the shard key in the update query automatically, so it works as expected.

  2. If you try to change the shard key, raise an exception. (or just use whatever pymongo raises)

In other words don't remove it from the delta automatically. If there's a bug in _delta, we need to fix it.

I want to avoid a weird case where you unexpectedly only save part of the changes to a document, which is what happens if you silently change the delta. That would confuse people.

We may need to fix our patch if we're modifying _delta.


I wasn't able to figure out a way to introspect the key. Probably need a documentation update as well. Basically you specify the shard key as a tuple.

@rozza rozza closed this issue from a commit
@rozza rozza Added Sharding support
Added shard_key meta, so save() and update() passes shard keys
to the pymongo query.  Also made shard key fields immutable.

Closes #388 and #389
@rozza rozza closed this in 11daf70

Once again - thanks for the patch @allang @andrewmlevy @crittercism

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.