2

I have this query which works fine in datagrip

test> db.getCollection("comments").find({status: {$ne: "APPROVED"},storyID: {$regex: "bbjfn-*"}})

I'm just wondering how to achieve the same thing in C# using the MongoDB Driver 2.13.1

IMongoDatabase database = MongoClient.GetDatabase(Program.Settings.MongoDB.Database);
IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("comments");

var filter = Builders<BsonDocument>.Filter.Eq("status", new BsonDocument("$ne", "APPROVED")) & 
Builders<BsonDocument>.Filter.Eq("storyID", new BsonDocument("$regex", "bbjfnfn-*"));

var results = await collection.FindAsync(filter);

Doesn't work.. what am I doing wrong?

1 Answer 1

5

You can set filter with BsonDocument object as below:

FilterDefinition<Store> filter = new BsonDocument
{
    { "status", new BsonDocument("$ne", "APPROVED") }, 
    { "storyID", new BsonDocument("$regex", "bbjfn-*") }
};

OR

var builder = Builders<BsonDocument>.Filter;
var filter = builder.Ne("status", "APPROVED") & builder.Regex("storyID", "bbjfn-*");

FYI, you can use MongoDB Compass to export Query to C# Language.

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.