MongoDB UpdateOne 没有更新记录

MongoDB UpdateOne is not updating a record

我在尝试更新单个记录时遇到了非常奇怪的行为。我使用 UpdateOne() 方法,它在 99% 的情况下都按预期工作,但有时我会得到以下结果: 如您所见,MongoDB 能够找到我的记录,但没有更新。我已经尝试更改写关注,根据文档,这可能会有所帮助:

collection.WithWriteConcern(WriteConcern.WMajority.With(journal: true))

但它没有。

这是我更新记录的方式:

collection.UpdateOne(x => x.Id == _myObject.Id.AsObjectId, updateDef);

更新定义:

var updateDef = new UpdateDefinitionBuilder<IndexedProfileData>().Set(x => x.Property.ChildCollection, newCollection);

如果有人能向我解释为什么会发生这种情况以及如何解决此问题,我将不胜感激。

MongoDB 不会更新已处于 "updated" 状态的文档。

例如,使用 mongo shell:

> db.test.find()
{"_id": 0, "a": 0}

> db.test.update({_id:0}, {$set:{a:1}})
WriteResult({
  "nMatched": 1,
  "nUpserted": 0,
  "nModified": 1
})

由于 a0 并且我们将 a 设置为 1,更新修改了文档(nMatched: 1nModified: 1).

> db.test.find()
{"_id": 0, "a": 1}

> db.test.update({_id:0}, {$set:{a:1}})
WriteResult({
  "nMatched": 1,
  "nUpserted": 0,
  "nModified": 0
})

如果我们再次尝试将 a 设置为 1,更新语句找到了该文档,但意识到它不需要做任何工作 (nMatched: 1 , nModified: 0).