Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So the previous implementation of `save` broke in the case that we tried to insert an entry with an `_id` field. ``` In [4]: list(db.test_coll.find()) Out[4]: [{'_id': ObjectId('5e672b717cc2bf7fb0f7adc6'), 'b': 201, 'c': 301}] In [9]: result = db.test_coll.replace_one({}, {'user_id': UUID('0e77ea51-184a-41e5-a025- ...: 9aa7f143d9f8'), '_id': ObjectId('584e66d088f6635c46b95a50'), 'data': {'reading': ...: 0.41276121139526367, 'name': 'POST_/usercache/get', 'ts': 1481533136.761428}, ' ...: metadata': {'write_fmt_time': '2016-12-12T00:58:56.761957-08:00', 'write_ts': 14 ...: 81533136.761957, 'time_zone': 'America/Los_Angeles', 'platform': 'server', 'writ ...: e_local_dt': {'hour': 0, 'month': 12, 'second': 56, 'weekday': 0, 'year': 2016, ...: 'timezone': 'America/Los_Angeles', 'day': 12, 'minute': 58}, 'key': 'stats/serve ...: r_api_time'}}, upsert=True) WriteError Traceback (most recent call last) WriteError: After applying the update, the (immutable) field '_id' was found to have been altered to _id: ObjectId('584e66d088f6635c46b95a50') ``` While this is not common in regular operation, it is used frequently in the tests when we load existing data with `_id` fields. So I had to modify the `save` command to treat the case with and without the `_id` field separately. If the update doc has an `_id` field, it must appear in the selection part of the method as well, even if there is no actual matching entry in the database. For example, the previous call works if we change it to ``` result = db.test_coll.replace_one({'_id': ObjectId('584e66d088f6635c46b95a50')} ...: , {'user_id': UUID('0e77ea51-184a-41e5-a025-9aa7f143d9f8'), '_id': ObjectId('58 ...: 4e66d088f6635c46b95a50'), 'data': {...}, 'metadata': {...}}) ``` Note that the entry was inserted (or rather upserted) instead of modified ``` In [12]: result.raw_result Out[12]: {'n': 1, 'nModified': 0, 'ok': 1.0, 'updatedExisting': False, 'upserted': ObjectId('584e66d088f6635c46b95a50')} ```
- Loading branch information