FindOneAndUpdateAsync 间歇性地返回 Null

FindOneAndUpdateAsync Intermittently Returning Null

我正在为 .NET Core 3.1 使用 MongoDB.Driver,运行 遇到一个问题,即记录未正确保存。在调用 FindOneAndUpdateAsync 时,它们间歇性地返回为 null。我有一个脚本调用我下面的代码 100 次。在这 100 个中,有 1-5 个在最后一个方法 SetChildFavoritesAsync 中失败。结果返回为空。对我做错了什么有什么建议吗?

调用示例[​​=13=]

var id = 1;
var childName = "test";
var collectionEntry = await FindByIdOrCreateAsync(id);

collectionEntry.Children = new List<MyCollection.ChildClass>{
  new MyCollection.ChildClass{
    Name = childName,
    Favorites = new List<MyCollection.ChildClass.Favorite>()
  }
};
await FindByIdAndUpdateChildrenAsync(collectionEntry.Id, collectionEntry.Children);

var favorites = new List<MyCollection.ChildClass.Favorite>{
  Name = "testFavorite"
};
var resultsOfSet = await SetChildFavoritesAsync(id, name, favorites)
//do stuff with resultsOfSet

示例模型

public class MyCollection
        {
            [MongoDB.Bson.Serialization.Attributes.BsonRepresentation(BsonType.ObjectId)]
            [MongoDB.Bson.Serialization.Attributes.BsonId]
            public string _Id { get; set; }
            [MongoDB.Bson.Serialization.Attributes.BsonRequired]
            public int Id { get; set; }
            public List<ChildClass> Children { get; set; }

            public class ChildClass
            {
                public string Name { get; set; }
                public List<Favorite> Favorites { get; set; }

                public class Favorite
                {
                    public string Name { get; set; }
                }
            }
        }

示例方法

public async Task<MyCollection> FindByIdOrCreateAsync(int id)
        {
            var filter = Builders<MyCollection>.Filter.Eq(mc => mc.Id, id);
            var update = Builders<MyCollection>.Update
                            .Set(mc => mc.Id, id)
                            .SetOnInsert(mc => mc.Children, new List<MyCollection.ChildClass>());
            var options = new FindOneAndUpdateOptions<MyCollection> { ReturnDocument = ReturnDocument.After, IsUpsert = true };
            return await _database.GetCollection<MyCollection>("MyCollectionName").FindOneAndUpdateAsync(filter, update, options);
        }

public async Task<MyCollection> FindByIdAndUpdateChildrenAsync(int collectionId, List<MyCollection.ChildClass> children)
        {
            var filter = Builders<MyCollection>.Filter.Eq(mc => mc.Id, collectionId);
            var update = Builders<MyCollection>.Update.Set(mc => mc.Children, children);
            var options = new FindOneAndUpdateOptions<MyCollection> { ReturnDocument = ReturnDocument.After, IsUpsert = false };
            return await _database.GetCollection<MyCollection>("MyCollectionName").FindOneAndUpdateAsync(filter, update, options);
        }

public async Task<MyCollection> SetChildFavoritesAsync(int collectionId, string childName, List<MyCollection.ChildClass.Favorite> favorites)
        {
            var filter = Builders<MyCollection>.Filter.Eq(mc => mc.Id, collectionId);
            filter &= Builders<MyCollection>.Filter.Eq("children.name", childName);
            var update = Builders<MyCollection>.Update.Set("children.$.favorites", favorites);
            var options = new FindOneAndUpdateOptions<MyCollection> { ReturnDocument = ReturnDocument.After };
            var results = await _database.GetCollection<MyCollection>("MyCollectionName").FindOneAndUpdateAsync(filter, update, options);
            if (results == null)
            {
                _log.Error($"Child Favorites didn't save: collectionId:{collectionId}, childName:{childName}");
            }
            else
            {
                _log.Debug($"Child Favorites: collectionId:{collectionId}, childName:{childName}, favorites:{Newtonsoft.Json.JsonConvert.SerializeObject(results)}");
            }
            return results;
        }

似乎是与数据库通信的问题。我添加了一些重试逻辑,解决了这个问题。