In this chapter, we’ll go through the syntax of various operations for mongodb updates.

Update of a single record

* update command is as follows: db. Test. UpdateOne ({" city_id ":" 298 "}, {$set: {" city_name ":" 5 "aba}}) 1. {" city_id" : "298"} for the query 2. {$set: {" city_name ":" 5 "aba}} for update statement execution results are as follows: {" acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }Copy the code
Db.test.deleteone ({"city_id" : "298"}); db.test.deleteone ({"city_id" : "298"}); 1.{"city_id" : "298"} is a query statement * and the result is as follows: {" indispensable ": true, "deletedCount" : 1}Copy the code

Updates to multiple records

Db.test. updateMany({"city_name" : "city_name"}, {$set: {"status" : 1}}); Db.test. update({"city_name" : "db.test.update",{$set: {"status" : 1}},false,true) true, "matchedCount" : 2, "modifiedCount" : 2 } 2. Db.test.deletemany ({"city_name" : "aba"}); {" indispensable ": true, "deletedCount" : 8} 3. Update ({"city_name" : "aba "},{$unset:{'status':1}}) Removes the status field from the documentCopy the code

Python crawlers implement batch execution of update statements

# -*- coding: UTF-8 -*- # @time: 20/1/15 6:09 PM # @author: cc # @filename: te.py from pymongo import UpdateOne from utils import mongo_util collection_test = mongo_util.get_clllection('test', 'test') update1 = UpdateOne({'_id': '299'}, {'$set': {"status": 1}}) update2 = UpdateOne({'_id': '301'}, {'$set': {"status": 0}}) write_list = [update1, update2] collection_test.bulk_write(write_list, ordered=False)Copy the code

The pit encountered during data update in python crawler, and the pit filling process

Update (<query>,<update>,<upsert>,<multi>), where <query> represents the filter condition, <update> is the data to be updated multi=True Updates all matched records multi=False Updates first matched records (default: False)Copy the code