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"
]

1 Answer 1

2

use $addToSet

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.