我正在尝试编写一个方法,将文档插入到我的 MongoDB 集合中(如果该文档尚不存在),否则更新整个文档(减去
_id
)。
查看了这个示例,但它已经有 5 年历史了,并且某些代码似乎已被弃用(并且它也不起作用):如果 mongodbDriver 已经存在,则更新整个文档
基于许多其他示例构建了以下内容:
public void CreateOrUpdate(Batch batch)
{
var filter = Builders<Batch>.Filter.Eq("_id", batch.Id);
var options = new ReplaceOptions { IsUpsert = true };
_batchCollection.ReplaceOne(filter, batch, options);
}
我尝试插入的类非常简单:
namespace MyApp.MyService.MyModel
{
[Serializable]
public class Batch
{
// Identification info
public string Description { get; set; } = "not provided";
// The unique Batch Id, this will be used everywhere to identify this batch run
[BsonId]
[BsonGuidRepresentation(GuidRepresentation.Standard)]
public Guid Id { get; set; }
[BsonGuidRepresentation(GuidRepresentation.Standard)]
public Guid AccountId { get; set; }
public string BatchName { get; set; } = "";
}
}
但是当我尝试插入第一个文档时,我得到:
MongoBulkWriteException`1:批量写入操作导致一个或多个错误。 WriteErrors: [ { Category : "Uncategorized", Code : 66, Message : "应用更新后,发现(不可变)字段 '_id' 已更改为 _id: UUID("bf46d19b-546f-4b96-8f0c- e06883b4d70d")" } ].
我之前证明了单个插入可以工作,因此具有连接并且客户端可以工作。
我可以正常工作,但我必须将插入和更新分开。即使这样,事情也不是一帆风顺的。我现在需要知道何时进行初始创建以及何时更新。我的问题是我正在进行并行操作,我希望让
MongdoDB
做出决定。这是客户端的最终代码。注意此代码的使用:
#pragma warning disable CS0618
BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
#pragma warning restore CS0618
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
这是我使用 GUID 作为我的 _id 来使过滤器工作的唯一方法。
完整代码如下所示:
using MyApp.MyService.Clients.Configuration;
using MyApp.MyService.Clients.Interfaces;
using MyApp.MyService.Models.ModelData;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
namespace MyApp.MyService.Clients
{
public class BatchClient : IBatchClient
{
private readonly IMongoCollection<Batch> _batchCollection;
public BatchClient(IOptions<MongoDBSettings> mongoDBSettings)
{
#pragma warning disable CS0618
BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
#pragma warning restore CS0618
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
MongoClient client = new MongoClient(mongoDBSettings.Value.ConnectionString);
IMongoDatabase database = client.GetDatabase(mongoDBSettings.Value.DatabaseName);
_batchCollection = database.GetCollection<Batch>("batch");
}
public void InsertAsync(Batch batch)
{
_batchCollection.InsertOneAsync(batch);
}
public void UpdateAsync(Batch batch)
{
var filter = Builders<Batch>.Filter.Eq(d => d.Id, batch.Id);
var options = new ReplaceOptions { IsUpsert = true };
_batchCollection.ReplaceOneAsync(filter, batch, options);
}
public void DeleteAsync(Guid id) {
var filter = Builders<Batch>.Filter.Eq("batchId", id);
_batchCollection.DeleteOne(filter);
}
public List<Batch> GetAll()
{
return _batchCollection.Find(f => true).ToList(); ;
}
}
}