There's a list List<BsonDocument>
with values like that:
{{ "_id" : 123, "IDD" : 123, "Sc" : { "Field1" : null, "Field2" : "some text|some text|"} }}
{{ "_id" : 124, "IDD" : 124, "Sc" : { "Field1" : { "fl" : "something" }, "Field2" : ""} }}
{{ "_id" : 125, "IDD" : 125, "Sc" : { "Field1" : { }, "Field2" : null} }}
It's was taken with simple filter like that:
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Gt("IDD", 122);
var collection = database.GetCollection<BsonDocument>("coll").Find(filter).ToList();
I have 2 quite similar questions:
How to get the element with this condition without the creation a class (i.e. just using BsonDocument):
SELECT * FROM coll WHERE Sc.Field2 != "" && Sc.Field2 != null
Expected result:
{{ "_id" : 123, "IDD" : 123, "Sc" : { "Field1" : null, "Field2" : "some text|some text|"} }}
How to get the element with this condition without the creation a class (i.e. just using BsonDocument):
SELECT * FROM coll WHERE Sc.Field1 != {} && Sc.Field2 != null
Expected result:
{{ "_id" : 124, "IDD" : 124, "Sc" : { "Field1" : { "fl" : "something" }, "Field2" : ""} }}
.
.
. I tried construction like that and it doesn't work:
filter &= !builder.ElemMatch<BsonValue>("Sc", new BsonDocument { { "Field2", BsonNull.Value } })
Found a solution. Somehow I've missed that syntax:
And it works for both cases. I hope it will be useful to everyone who encounters the same case.