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?
CodePudding user response:
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.