On MongoDB 6 with an empty collection, running these queries won’t work:
<COLLECTION>.update_one({'list': 'a'}, {'$addToSet': {'list': 'a'}}, upsert=True)
<COLLECTION>.update_one({'list': {'$in': ['a']}}, {'$addToSet': {'list': 'a'}}, upsert=True)
Only if the document doesn’t exist, will MongoDB assume the field is a string and raise this error:
Cannot apply $addToSet to non-array field. Field named 'list' has non-array type string
These will work because MongoDB assumes the key is an array:
<COLLECTION>.update_one({'list': ['a']}, {'$addToSet': {'list': 'a'}}, upsert=True)
<COLLECTION>.update_one({'list': {'$in': ['a', 'b']}}, {'$addToSet': {'list': 'a'}}, upsert=True)
I tried searching for the solution everywhere and people say it’s solved around MongoDB 3.2~ which it probably either wrong or a fix for a different issue.
It’s probably not related to ‘pymongo’, but that’s the package I used.
Does anyone know a good solution or how to get around this issue?