$addToSet
https://www.mongodb.com/docs/manual/reference/operator/update/addToSet/
[
{
"name": "s1",
"skills": ["python", "shell"]
},
{
"name": "s2",
"skills": ["perl", "r"]
}
]
# 更新单个元素到列表,自动去重
_update = {
'$addToSet': [
'skills': 'perl'
]
}
col.update({'name': 's1'}, _update) # skills: ['python', 'shell', 'perl']
# 更新列表素到列表,自动去重
_update = {
'$addToSet': [
'skills': {'$each': ['perl', 'r']}
]
}
col.update({'name': 's1'}, _update) # skills: ['python', 'shell', 'perl', 'r']