Nodejs 求问node-mongodb-native里,那个w参数是什么意思?

Nodejs 求问node-mongodb-native里,那个w参数是什么意思?

w, {Number/String, > -1 || ‘majority’ || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = ‘majority’ or tag acknowledges the write 基本上所有的函数里的w都这么描述,但是这个真的看不懂。

求解释。

另外这个参数跟safe:ture/false有关系吗?


7 回复

在使用 node-mongodb-native 库与 MongoDB 进行交互时,w 参数是一个重要的写关注(write concern)参数。它用于确保写操作在被确认为成功之前,数据已复制到指定数量的副本集成员上。这有助于确保数据的持久性和一致性。

w 参数详解

  • w: <number>: 当你设置一个数字(如 w: 1),这意味着操作将在确认至少一个副本集成员已经接收到数据后返回。

  • w: 'majority': 这意味着操作将在确认大多数副本集成员已经接收到数据后返回。这通常用于确保更高的数据一致性。

  • w: 'tag name': 如果你的副本集配置了标签,你可以通过设置标签名称来指定特定的成员或组。

示例代码

const MongoClient = require('mongodb').MongoClient;
const uri = "your_mongodb_connection_string_here";

MongoClient.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
    if (err) throw err;

    const db = client.db("your_database_name");
    const collection = db.collection("your_collection_name");

    // 插入文档并设置写关注
    collection.insertOne({ name: "John Doe" }, { w: 1 }, (err, result) => {
        if (err) throw err;
        console.log("Document inserted with w: 1");
        client.close();
    });

    // 插入文档并设置写关注为 'majority'
    collection.insertOne({ name: "Jane Doe" }, { w: 'majority' }, (err, result) => {
        if (err) throw err;
        console.log("Document inserted with w: 'majority'");
        client.close();
    });
});

w 参数与 safe 参数的关系

在较旧版本的 node-mongodb-native 中,safe 参数用于设置写关注。如果 safe: true,则默认的写关注是 w: 1,即至少一个副本集成员需要确认写操作。而新版本中推荐使用 w 参数来明确指定写关注级别。

因此,w 参数更灵活且更具可读性,可以精确控制数据的持久性和一致性。


当你遇到要用这个就明白了

我原来一直用mongoskin,但是最近发现他有问题,就去看原生的驱动。但是发现这里有这样一个参数,但是始终看不明白。 有朋友给解释一下吗?

safe:ture/false以及被淘汰了,这是过去的参数,现在用w表示,w=0相当于saft=false的形式,当然w能够表达的存储方式已经超越了saft=true,w=1表示只有一个节点写入成功即返回,w=2表示两个节点写入成功才返回,更多w的含义请查看官方文档.一般w=1就可以了

友情提示一下w是write的简写形式哈哈

w参数是write concern 看官网描述

write concern Specifies whether a write operation has succeeded. Write concern allows your application to detect insertion errors or unavailable mongod instances. For replica sets, you can configure write concern to confirm replication to a specified number of members.

node-mongodb-native 驱动中,w 参数用于设置写操作的写关注(write concern)。写关注定义了MongoDB必须确保复制的数据副本数量或特定节点组的数量。这有助于确保数据写入后的持久性和一致性。

  • w = 1 或者没有指定 w 参数时,只等待主节点确认写操作完成。这是默认行为。
  • w > 1 表示需要等待至少 w 个副本集成员确认写操作。
  • 'majority' 表示需要等待大多数副本集成员确认写操作。
  • w 可以是字符串形式的标签名,表示需要等待带有该标签的节点确认写操作。

示例代码

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/';

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  const dbo = db.db("mydb");
  
  // 设置 w = 2,表示需要等待至少两个副本确认写操作
  dbo.collection("customers").insertOne({ name: "John", address: "Highway 37" }, { w: 2 }, function(err, res) {
    if (err) throw err;
    console.log("1 document inserted with w=2");
    db.close();
  });
});

safe 的关系

在较旧版本的 node-mongodb-native 中,safe 参数用于设置是否检查写操作的结果。在新版本中,推荐使用 w 参数替代 safe 参数来实现相同的功能。如果你同时设置了 safeww 会覆盖 safe 的设置。

总结来说,w 参数允许你控制写操作的确认条件,确保数据在多个副本间同步,从而提高系统的可靠性和一致性。

回到顶部