意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

mongodb事务

来源:恒创科技 编辑:恒创科技编辑部
2023-12-05 19:02:59
mongo开启事物支持如下配置:
/**
* Configuration options for a transaction.
* @see https://mongodb.github.io/node-mongodb-native/3.6/api/global.html#TransactionOptions
*/
export interface TransactionOptions {
readConcern?: ReadConcern;
writeConcern?: WriteConcern;
readPreference?: ReadPreferenceOrMode;
}

写操作
/**
* A MongoDB WriteConcern, which describes the level of acknowledgement
* requested from MongoDB for write operations.
* @see https://mongodb.github.io/node-mongodb-native/3.6/api/global.html#WriteConcern
*/
interface WriteConcern {
/**
* requests acknowledgement that the write operation has
* propagated to a specified number of mongod hosts
* @default 1
*/
w?: number | 'majority' | string;
/**
* requests acknowledgement from MongoDB that the write operation has
* been written to the journal
* @default false
*/
j?: boolean;
/**
* a time limit, in milliseconds, for the write concern
*/
wtimeout?: number;
}

什么是writeConcern?

mongodb事务_mongodb


mongodb事务

majority:超过半数以上的节点才算成功,常用于经常被扩容的节点

默认行为

mongodb事务_github_02

写入x=1立即返回,并没有等到从节点复制完成

使用majority

mongodb事务_github_03

写入x=1,等到从节点1完成复制,再返回, 因为3个节点,所以2个节点成功就可以返回了

j:true

mongodb事务_mongodb_04

写到journal文件才返回

mongodb事务_mongodb_05

注意事项

mongodb事务_github_06

读操作

mongodb事务_html_07

export type ReadPreferenceTags = ReadonlyArray<Record<string, string>>;
export type ReadPreferenceMode = 'primary' | 'primaryPreferred' | 'secondary' | 'secondaryPreferred' | 'nearest';
export type ReadPreferenceOrMode = ReadPreference | ReadPreferenceMode;
export type ReadPreferenceOptions = {
/** Server mode in which the same query is dispatched in parallel to multiple replica set members. */
hedge?: {
/** Explicitly enable or disable hedged reads. */
enabled?: boolean;
};
/**
* Max secondary read staleness in seconds, Minimum value is 90 seconds.
*/
maxStalenessSeconds?: number;
};

/**
* The **ReadPreference** class represents a MongoDB ReadPreference and is used to construct connections.
* @see https://docs.mongodb.com/manual/core/read-preference/
*/
export class ReadPreference {
constructor(mode: ReadPreferenceMode, tags: object, options?: ReadPreferenceOptions);
mode: ReadPreferenceMode;
tags: ReadPreferenceTags;
static PRIMARY: 'primary';
static PRIMARY_PREFERRED: 'primaryPreferred';
static SECONDARY: 'secondary';
static SECONDARY_PREFERRED: 'secondaryPreferred';
static NEAREST: 'nearest';
isValid(mode: ReadPreferenceMode | string): boolean;
static isValid(mode: string): boolean;
/**
* Indicates that this readPreference needs the "slaveOk" bit when sent over the wire
* @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
*/
slaveOk(): boolean;
/**
* Are the two read preference equal
* @param readPreference - the read preference with which to check equality
* @return `true` if the two {@link ReadPreference}s are equivalent
*/
equals(readPreference: ReadPreference): boolean;
}

/** @see https://mongodb.github.io/node-mongodb-native/3.6/api/global.html#ReadConcern */
type ReadConcernLevel = 'local' | 'available' | 'majority' | 'linearizable' | 'snapshot';

/**
* The MongoDB ReadConcern, which allows for control of the consistency and isolation properties
* of the data read from replica sets and replica set shards.
* @see https://mongodb.github.io/node-mongodb-native/3.6/api/global.html#ReadConcern
*/
export interface ReadConcern {
level: ReadConcernLevel;
}

什么是readPreference

mongodb事务_github_08

readPreference场景举例

mongodb事务_github_09

readPreference与Tag

mongodb事务_github_10

readPreference配置

mongodb事务_html_11

实验

mongodb事务_mongodb_12

注意事项

mongodb事务_github_13

什么是readConcern

mongodb事务_mongodb_14

local和available

mongodb事务_html_15

注意事项

mongodb事务_mongodb_16

readConcern:majority

mongodb事务_html_17

majority实现方式

mongodb事务_mongodb_18

实验


mongodb事务_github_19


mongodb事务_mongodb_20

readConcern:如何实现安全的读写分离

mongodb事务_mongodb_21

readConcern:linearizable

mongodb事务_mongodb_22

选举过程中,同时old节点还能工作

snapshot

mongodb事务_mongodb_23

多文档事务

4.0只支持复制集,不支持分片

mongodb事务_html_24


mongodb事务_mongodb_25

使用方法

mongodb事务_github_26

事务的隔离级别

mongodb事务_github_27

实验

mongodb事务_html_28

可重复读

mongodb事务_github_29

事务写机制


mongodb事务_mongodb_30


mongodb事务_github_31

等另一个提交后,重启事务就好了 session.abortTransaction()


mongodb事务_mongodb_32

注意事项

mongodb事务_mongodb_33




上一篇: mysql直接拷贝data目录下数据库源文件还原数据库方法 下一篇: JavaScript单线程和任务队列是什么