Good day! I’m trying to make such a thing - when i’m upserting data if want to give a field default value if there was not such field and now field equals null. I’m trying to use $isNull operator.
var enrichmentsUpdateModels = enrichments.Select(m =>
{
var filter = new BsonDocument("_id", m["_id"]);
var setBsonDoc = new BsonDocument("type", m["type"]);
foreach (var s in m.Names.Where(n=>n.StartsWith("data")))
{
if (s is not null)
{
if (m[s].IsBsonNull)
{
setBsonDoc.AddRange(new BsonDocument(s,new BsonDocument("$ifNull", new BsonArray
{
$"${s}", ":"
})));
}
else
{
setBsonDoc.AddRange(new BsonDocument(s,m[s]));
}
}
}
var updateSettings = new BsonDocument("$set", setBsonDoc);
return new UpdateOneModel<BsonDocument>(filter, updateSettings) {IsUpsert = unexistingEmails};
});
I want to save old value if the field in db when i’m upserting a null value. But i want to upsert a null value if there no such filed in db at all
But when I use $ifNull - db creates a field named “$ifNull” and sets an array there