0

I have one query in MongoDB like below,

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

How to convert this query to execute from C# code?

I tried using the below code but that didn't work. Can you guide me?

var builder = Builders<BsonDocument>.Filter;

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

Sample Mongo docs - & the expected outcome should be doc2

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

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

1 Answer 1

1

You can apply the query as BsonDocument.

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

Output

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.