Di MongoDB 4.0 Anda dapat menggunakan $[<identifier>]
sintaks
dan tambahkan ArrayFilters
ke UpdateOptions
parameter:
var filter = Builders<Model>.Filter.Empty;
var update = Builders<Model>.Update.Set("Options.$[option].Inventory.$[inventory].Name", "New name");
var arrayFilters = new List<ArrayFilterDefinition>();
ArrayFilterDefinition<BsonDocument> optionsFilter = new BsonDocument("option.Id", new BsonDocument("$eq", optionId));
ArrayFilterDefinition<BsonDocument> inventoryFilter = new BsonDocument("inventory.Id", new BsonDocument("$eq", inventoryId));
arrayFilters.Add(optionsFilter);
arrayFilters.Add(inventoryFilter);
var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };
var result = DefaultCollection.UpdateOne(filter, update, updateOptions);
Itu akan secara unik mengidentifikasi Inventory
item yang perlu diperbarui di dalam Options