0

I am using $push to appends a value to an array.

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,

"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"
]
daiyue
  • 7,196
  • 25
  • 82
  • 149

1 Answers1

2

use $addToSet

connection.erp_datasets.erp_datasets.update_one({'erp_name': 'erp1'}, {'$addToSet': {'data_set': 'database1'}}, upsert=True)

bgraham
  • 1,939
  • 1
  • 10
  • 17