不等于 C# 中 MongoDB 查询的运算符

Not equal to operator for MongoDB query in C#

我在 MongoDB 中有一个查询,如下所示,

db.getCollection('Student')
.find({_id: 123, $where:"this.section != this.upperSection"})

如何将此查询转换为从 C# 代码执行?

我尝试使用以下代码,但没有用。你能指导我吗?

var builder = Builders<BsonDocument>.Filter;

var filter = builder.Not("section ", "upperSection");

示例 Mongo 文档 - 预期结果应为 doc2

//doc1
{
    "_id" : "123",
    "section" : "X",
    "upperSection" : "X"
}

//doc2
{
    "_id" : "123",
    "section" : "X",
    "upperSection" : "Y"
}

您可以将查询应用为 BsonDocument

FilterDefinition<BsonDocument> filter = new BsonDocument("$expr", 
    new BsonDocument("$ne",
        new BsonArray { "$section", "$upperSection" }
    )
);

Output