Updating Multiple Documents in mongodb using mongoengine

Viewed 45

I have a list of dictionary like

{'data': '[{"url":"https://example1.com/photos-reviews/","score":0.671},{"url":"https://example1.com/concerts-events/concert-organ-performances/2018-2019/","score":0.662},{"url":"https://www.example2.org/support-volunteer/orchestra-league/","score":0.645}]', 'type': 'invalid'}

The field url is unique and based on the field type (invalid or valid - Here invalid), the field hum_proc in the mongodb which is by default null will be changed to invalid or valid respectively.

I did this by

@sources_api.route('pending_val', methods=["GET", "POST"])
def pending_val():
     if request.method=='POST':
         data_rec = request.get_json()
         if 'type' in data_rec:
             data_list= json.loads(data_rec["data"])
             for val in data_list:
                MlData.objects(url=val["url"]).update_one(set__hum_proc=data_rec["type"])

I am iterating and updating each item. Is it possible to update this in a single step more efficiently using aggregation pipeline or so

1 Answers

You can use update() method to perform atomic update on fields matched by your query:

MlData.objects(<your_query>).update(set__hum_proc=data_rec["type"])

UPDATE:

update_one() method overwrites or adds first document matched by query, whereas update() performs update on multiple fields.

I understand you want to update urls from request body with equivalent type value. You can do that pretty much in one line:

MlData.objects(url__in=[e['url'] for e in data_rec['data']]).update(set__hum_proc=data_rec["type"])

There are couple notes here.

Firstly, I altered your request json, removing redundant quotes in data key, making it more readable:

data_rec = {'data': [{"url": "https://example1.com/photos-reviews/",
                      "score": 0.671},
                     {"url": "https://example1.com/concerts-events/concert-organ-performances/2018-2019/",
                      "score": 0.662},
                     {"url": "https://www.example2.org/support-volunteer/orchestra-league/",
                      "score": 0.645}],
            'type': 'valid'}

Secondly, I created list of url from that json on the fly, which finally allowed me to find those exact values in the database using in query operator.

Related