Hi, I am trying to filter on an array type, and then perform an upsert.
using MongoDB.Bson;
using MongoDB.Driver;
class MyModel
{
public ObjectId _id;
public string[]? myArray;
public int? myInt;
}
public class UnitTest1
{
[Fact]
public async void Test1()
{
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("MyDatabase");
var collection = database.GetCollection<MyModel>("MyCollection");
var update = Builders<MyModel>.Update.Set(x => x.myInt, 5);
var filter = Builders<MyModel>.Filter.AnyEq(x => x.myArray, "not_found");
var options = new FindOneAndUpdateOptions<MyModel>
{
IsUpsert = true,
ReturnDocument = ReturnDocument.After
};
MyModel result = collection.FindOneAndUpdate(filter, update, options); // fails to deserialize
Assert.IsNotType<string>(result.myArray);
}
}
Currently it is throwing an error because it seems like it is trying to upsert the document with the “myArray” field set to a string type causing a type mismatch. Can someone explain to me how I can achieve the upsert with the array filter?