如何更新数据

对于已有一个MongoDB的记录,想要去更新其中的部分数据,有如下几种方式:

MongoDB shell

官网举例

db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );

举例:设置active为Y

mongo shell中去插入新字段

db.question.update({}, {$set: {"active": "Y"}}, {upsert: false, multi: true})

输出:

> db.question.update({}, {$set: {"active": "Y"}}, {upsert: false, multi: true})
WriteResult({ "nMatched" : 883, "nUpserted" : 0, "nModified" : 883 })

效果:

mongodb_compass_updated_active_y

pymongo

通过PyMongo去更新某个记录的部分数据

举例:

        insertResult = mongoDstCollection.insert_one(dstBookInfo)
        logging.debug("insertResult=%s", insertResult)
        newDstRecordId = insertResult.inserted_id
        logging.debug("newDstRecordId=%s", newDstRecordId)
        # update inserted id into existing old source record
        mergedId = ""
        if newDstRecordId:
            mergedId = str(newDstRecordId)
        logging.debug("mergedId=%s", mergedId) # '5bd28426bfaa44216e98a496'
        updateResult = mongoSrcCollection.update_one(filter={"_id": curSrcBookIdOjb}, update={"$set": {"mergedId": mergedId}})
        logging.debug("updateResult=%s", updateResult)
        matched_count = updateResult.matched_count
        modified_count = updateResult.modified_count
        logging.debug("matched_count=%s, modified_count=%s", matched_count, modified_count)

results matching ""

    No results matching ""