/**写操作
* 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;
}
/**什么是writeConcern?
* 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;
}
majority:超过半数以上的节点才算成功,常用于经常被扩容的节点
默认行为写入x=1立即返回,并没有等到从节点复制完成
使用majority写入x=1,等到从节点1完成复制,再返回, 因为3个节点,所以2个节点成功就可以返回了
j:true写到journal文件才返回
注意事项 读操作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 */什么是readPreference readPreference场景举例 readPreference与Tag readPreference配置 实验 注意事项 什么是readConcernlocal和available 注意事项 readConcern:majority majority实现方式 实验
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;
}
选举过程中,同时old节点还能工作
snapshot多文档事务4.0只支持复制集,不支持分片
等另一个提交后,重启事务就好了 session.abortTransaction()