official documentation
- PyMongo : There is only API interface, and the settings of query, filtering, etc. are in the official documentation of Mongodb.
- Mongodb official documentation
- CRUD
- exhaustive documentation
- OPERATOR (upsert, etc.)
- CRUD
training
Transactions in Mongodb
MySQL transactions
MySQL transactions are mainly used to process data with a large amount of operations and high complexity. For example, in the personnel management system, if you delete a person, you need to delete both the basic information of the person and the information related to the person, such as mailboxes, articles, etc. In this way, these database operation statements constitute a transaction !
- Transactions are supported in MySQL only for databases or tables that use the Innodb database engine.
- Transaction processing can be used to maintain the integrity of the database, ensuring that batches of SQL statements are either all executed or none of them are executed.
- Transactions are used to manage insert, update, delete statements
Generally speaking, a transaction must meet four conditions (ACID): Atomicity (or indivisible), Consistency, Isolation (Isolation, also known as Independence), Durability .
Atomicity: All operations in a transaction are either completed or not completed, and will not end at a certain link in the middle. If an error occurs during the execution of the transaction, it will be rolled back (Rollback) to the state before the transaction started, as if the transaction was never executed.
Consistency: The integrity of the database is not violated before the transaction begins and after the transaction ends. This means that the written data must fully comply with all the preset rules, including the accuracy of the data, the concatenation and the subsequent database can spontaneously complete the predetermined work.
Isolation: The ability of the database to allow multiple concurrent transactions to read, write and modify its data at the same time. Isolation can prevent data inconsistency due to cross execution when multiple transactions are executed concurrently. Transaction isolation is divided into different levels, including read uncommitted (Read uncommitted), read committed (read committed), repeatable read (repeatable read) and serializable (Serializable).
Persistence: After the transaction is completed, the modification of the data is permanent and will not be lost even if the system fails.
example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
#包的导入
db = MySQLdb.connect("ip地址,本机为localhost","用户名","密码","表名")
#打开数据库的连接
cursor = db.cursor()
#使用cursor()方法获得操作游标
try:
# 执行sql语句
cursor.execute("update account set money=money-600 where name='zhangsan'")
cursor.execute("update account set money=money+600 where name='lisi'")
# 提交到数据库执行
db.commit()
except:
# 发生错误时回滚 回滚到获取游标的位置开始重新执行 看代码上面的文字有说明
db.rollback()
db.close()
#关闭数据库的连接
Mongodb is also about to add transaction features
type of data
ObjectID
important operations
Inquire
find
update data
update
db.COLLECTION_NAME.update({},{},true|false,true|false);
- The first parameter is the query selector, which is the same as the parameter of findOne, which is equivalent to the where clause of sql
- The second parameter is the update operation file, which consists of various update operators and update values.
- The third parameter is upsert. If it is true, it means that if there is no document matching the query selector, mongo will combine the first and second parameters to insert a new document into the collection.
- The fourth parameter is multi. true: update all matched documents, false: update the first matched document, default value
findAndModify
At most one document is updated at a time, that is, the conditional query condition, and the first document after sorting is executed.
db.COLLECTION_NAME.findAndModify({query:{}, update:{}, remove:true|false, new:true|false, sort:{}, fields:{}, upsert:true|false});
- query is a query selector, the same as findOne's query selector
- update is the value to be updated, it cannot appear at the same time as remove
- remove means to delete documents that meet the query conditions, and cannot appear at the same time as update
- new is true: return the updated document, false: return before the update, the default is false
- sort: Sort condition, consistent with the parameters of the sort function.
- fields: consistent with the second parameter of find*, indicating which fields to return
- upsert: The same as the upsert parameter of update.
The difference between the usefulness of update and findandmodify
with findAndModify you increment the counter and get its incremented value in one step.
Compare if A perform this operation in two steps (update and get value) and B does the same operation between your steps
then A and B may get the same last counter value instead of two different (just one example of possible issues).using update to modify a single document i.e.(“multi”:false} is also atomic. Currently, it should also be faster than doing the equivalent update using findAndModify.