I need to construct the following query using MongoDB C# driver
db.Notes.find({ "Group._id" : 74, "CustomFields" : { "$elemMatch" : { "Value" : /batch/i } }, "IsDeleted" : false }).sort({ "CreatedDateTimeUtc" : -1 })
I used a query like this
builder.ElemMatch(x => x.CustomFields, x => x.Value.Contains(filterValue))
It generated mongo query as
db.Notes.find({ "Group._id" : 74, "CustomFields" : { "$elemMatch" : { "Value" : /batch/s } }, "IsDeleted" : false }).sort({ "CreatedDateTimeUtc" : -1 })
if you notice it is appending s at /batch/s
instead of i /batch/i
How can I get this work? I need to do this for filters like
Can I do something like this, so that I can apply all my regex patterns for all above filters.
builder.Regex(x => x.CustomFields[-1].Value, new BsonRegularExpression($"/{filterValue}/i"));
This converts the query to as below, but that doesn't get any results
db.Notes.find({ "Project._id" : 74, "CustomFields.$.Value" : /bat/i, "IsDeleted" : false }).sort({ "CreatedDateTimeUtc" : -1 })
FYI: builder
is FilterDefinition<Note>
My sample Notes Collection is like this:
{
Name:"",
Email:"",
Tel:"",
Date:02 /21/1945,
CustomFields:[
{
Name:"",
Value:"",
IsSearchable:true,
},
{
Name:"",
Value:"",
IsSearchable:true,
},
{
Name:"",
Value:"",
IsSearchable:true,
},
{
Name:"",
Value:"",
IsSearchable:true,
}
]
}
As you already know, C# is a general-purpose language and MongoDB is a general-purpose data platform. Together, C# and MongoDB are a powerful combination.
The MongoDB C Driver, also known as “libmongoc”, is a library for using MongoDB from C applications, and for writing MongoDB drivers in higher-level languages. It depends on libbson to generate and parse BSON documents, the native data format of MongoDB.
Welcome to the documentation site for the official MongoDB C++ driver. You can add the driver to your application to work with MongoDB using the C++11 or later standard. Download the library, mongocxx , from mongocxx.org or set up a runnable project by following our tutorial.
Short answer - no, it's for sure possible, but not reasonable. MongoDB is document database and not support any physical relations between collections. EF is a good fit for relational databases like SQL, MySQL, etc. MongoDB works faster with embedded documents.
It sounds like all you're missing is the insensitive part. Have you tried this?
ToLower, ToLowerInvariant, ToUpper, ToUpperInvariant (string method) These methods are used to test whether a string field or property of the document matches a value in a case-insensitive manner.
According to the 1.1 documentation here, it says that will allow to perform a case insensitive regex match. The current documentation doesn't mention it, so just to be sure, i checked github and the code to create an insensitive match is still there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With