问题描述
我的Web服务写入mongodb,我注意到在负载下,写入失败很多.阅读本文档我看到默认情况下,设置被设置为"Acknowledge",并阅读看来我需要将其设置为更高的设置(安全模式-似乎已弃用).所以我的问题是,我应该如何初始化我的mongodb,以便我的Web服务将总是"写入数据库(或尝试抛出错误)并且不会失败(假设它可以写:))
My web service writes to a mongodb and I notice that under load, the writes are failing a lot.Reading this documentation I see that by default the settings are set to "Acknowledge" and reading this post It seems I need to set it to a higher setting (safe mode - which seems to be deprecated). So my question is , how should I initialize my mongodb such that my web service will "always" write to the db (or throw an error trying) and not fail (assuming it can write :) )
据我了解,我需要设置写关注",但不清楚如何将其设置为确保成功写入数据库"
From what I understand I need to set the "write concern" but not clear on how to set it to "Assure success in writing to the db"
我现在用来检索Db的代码是:
The code I am using now to retrieve the Db is :
MongoClient client = new MongoClient(connection_string);
var server = client.GetServer();
Database = _db = server.GetDatabase(dbname);
推荐答案
为此,您的连接字符串必须具有选项w = 1,这是更高版本的驱动程序中的默认选项.连接字符串中的内容如下所示:
Your connection string needs to have the option w=1 for this, which is the default in later versions of the driver. This looks like the following in the connection string:
mongodb://localhost/?w=1
但这是默认设置.
您写道写入失败很多"-您如何看待?是由于文档不在数据库中,还是由于出错?让我知道,我将更新答案.
You write that "writes are failing a lot" - how do you see that? By documents not being in the database, or by getting errors? Let me know and I'll update the answer.
这篇关于C#mongodb驱动程序写问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!