I am trying to access MongoDB from C# ASP.NET application.
Let's assume, I've a document like below-
{
"_id" : ObjectId("546c776b3e23f5f2ebdd3b03"),
"Name" : "Test",
"Values" : [
{
"Name" : "One",
"Value" : 1
},
{
"Name" : "Two",
"Value" : 2,
"Parameters": [{"Type": "Type A"}, {"Type": "Type B"}]
}
]
}
Please note that, only the _id
and Name
elements are fixed; other elements are dynamically created by the user where both the key and value are defined by the user.
Now, I would like to search for the element Type
with the value Type A
. How can I do this from a MongoDB C# driver?
Below aggregation query may solve your problem but I don't know how to write this in C#
You can use this code:
If you data has structure use this:
Edit: For dynaically search the only way comes to my mind is full-text-search:
Step1: Define a full text-search on all fields via
db.test.ensureIndex({"$**" : "text"});
Step2: Search your query
db.test.find( { $text: { $search: "Type A" } } )
If its your answer, the C# code should be easy.