更新后 SafeModeResult 为 null
使用 MongoDB 和最新的 10gen C# 驱动程序 (CSharpDriver-1.3.1.4349),我尝试进行“就地”更新并取回结果中受影响的文档数量。
public static long SaveListings(string state, bool isActive, DateTime updateDate)
{
var result = Collection().Update(
Query.And(
Query.EQ("State", state),
Query.And(
Query.EQ("IsActive", isActive),
Query.LT("UpdateDate", updateDate))),
Update.Set("IsActive", false), UpdateFlags.Multi);
return result != null ? result.DocumentsAffected : -1;
}
由于某种原因,结果为空。如果我从控制台执行此操作,我可以获得这样做所影响的行数:
db.Listing.update( { State: state.Abbreviation, IsActive: true, UpdateDate: { $lt: expiredDate } }, { $set: { IsActive: false } }, false, true);
var numRows = db.getLastErrorObj().n;
知道我做错了什么还是这是 C# 驱动程序中的错误?
Using MongoDB and the latest 10gen C# driver (CSharpDriver-1.3.1.4349), I am trying to do an "in place" update and get back the # of documents effected in the result.
public static long SaveListings(string state, bool isActive, DateTime updateDate)
{
var result = Collection().Update(
Query.And(
Query.EQ("State", state),
Query.And(
Query.EQ("IsActive", isActive),
Query.LT("UpdateDate", updateDate))),
Update.Set("IsActive", false), UpdateFlags.Multi);
return result != null ? result.DocumentsAffected : -1;
}
The result is null for some reason. If I were doing this from the console, I could get the number of rows effected by doing this:
db.Listing.update( { State: state.Abbreviation, IsActive: true, UpdateDate: { $lt: expiredDate } }, { $set: { IsActive: false } }, false, true);
var numRows = db.getLastErrorObj().n;
Any idea what I'm doing wrong or is this a bug in the C# driver?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新包含采用
SafeMode
的重载方法。只需将其作为更新的第四个参数添加到您的代码中,并且不应为空:这不是驱动程序错误,它会按预期工作。如果在没有安全模式的情况下插入,Mongodb 不会等待文档(因此驱动程序返回 null),但如果您说 SafeMode = true - 您会强制 mongodb 等待文档插入。
Update contains overloaded method that take
SafeMode
. Just add it to your code as fourth parameter to your update and should be not null:That's not driver bug, it work as expected. Mongodb does not wait for document if get inserted without safe mode (therefore driver return null), but if you saying SafeMode = true -- you force mongodb wait until document get inserted.
在不指定安全模式的情况下执行插入和更新会产生 null,因为它们是异步操作 - 根本无法知道插入或更新是如何进行的。
因此,如果您关心结果,请添加 SafeMode.True 作为插入和更新中的最后一个参数。这将使驱动程序发出
getLastError
命令,该命令会阻塞,直到插入/更新完成:您可以阅读有关
getLastError
和不同安全模式的更多信息 此处(一般) 和 此处(带有 C# 驱动程序的SafeMode
)。Doing inserts and updates without specifying safe mode yields null because they're asynchronous operations - there's simply no way to know how the insert or update went.
Therefore, add e.g. SafeMode.True as the last argument in inserts and updates in cases where you care about the result. This will make the driver issue a
getLastError
command, which blocks until the insert/updated has completed:You can read some more about
getLastError
and different safe modes here (general) and here (SafeMode
with the C# driver).在这里尝试 getLastError: http://www.webtropy.com/articles/art9 -1.asp?f=GetLastError
Try getLastError here: http://www.webtropy.com/articles/art9-1.asp?f=GetLastError