Certain key values are lost with no warning when using $set to update a document. The following snippet illustrates:
import pymongo
document = {}
for x in range(3000):
document['%04X' % x] = "Hello"
collection = pymongo.Connection().test_db.data
spec = dict(_id = "unique_id")
- Make sure document is empty
collection.remove(spec)
- Make a new document
collection.save(spec)
print "Length of document %s" % len(document)
collection.update(spec,
{"$set": document}, upsert=False, safe=True)
result = collection.find_one(spec)
print "Length of stored data %s" % len(result)
for key in document:
if key not in result:
print "Key %s is missing" % key
This is the output
Length of document 3000
Length of stored data 2981
Key 00A7 is missing
Key 00A6 is missing
Key 00A5 is missing
Key 00A4 is missing
Key 00A3 is missing
Key 00A2 is missing
Key 00A1 is missing
Key 00A0 is missing
Key 00A9 is missing
Key 00A8 is missing
Key 00B2 is missing
Key 00B3 is missing
Key 00B0 is missing
Key 00B1 is missing
Key 00B6 is missing
Key 00B7 is missing
Key 00B4 is missing
Key 00B5 is missing
Key 00B8 is missing
Key 00B9 is missing
Changing the formatter from %04X to %04d seems to bypass the issue. So there is something about a pattern in the text.