[英]mongodb how to maintain an unique array
I am using $push
to appends a value to an array. 我正在使用$push
将值附加到数组。
connection.erp_datasets.erp_datasets.update_one({'erp_name': 'erp1'},
{'$push': {'data_set': 'database1'}}, upsert=True)
The problem is whenever I run the query again, multiple erp1
will be appended to the list, 问题是,每当我再次运行查询时, erp1
将多个erp1
附加到列表中,
"data_set" : [
"erp1",
"erp1"
]
I am wondering how to maintain the array with unique values. 我想知道如何维护具有唯一值的数组。 so it doesn't matter how many times I executed the above query, data_set
will only contain one erp1
, 因此我执行上述查询多少次都没有关系, data_set
仅包含一个erp1
,
"data_set" : [
"erp1"
]
use $addToSet 使用$ addToSet
connection.erp_datasets.erp_datasets.update_one({'erp_name': 'erp1'}, {'$addToSet': {'data_set': 'database1'}}, upsert=True) connection.erp_datasets.erp_datasets.update_one({'erp_name':'erp1'},{'$ addToSet':{'data_set':'database1'}},upsert = True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.