Find an specific element in a MongoDB document from C# -
i trying access mongodb c# asp.net application.
let's assume, i've document 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, _id
, name
elements fixed; other elements dynamically created user both key , value defined user.
now, search element type
value type a
. how can mongodb c# driver?
you can use code:
var query = query.eq("values.parameters.type", "type a"); var items = collection.find(query).tolist();
if data has structure use this:
var items = collection.findas<item>(query).tolist();
edit: dynaically search way comes mind full-text-search:
step1: define full text-search on fields via db.test.ensureindex({"$**" : "text"});
step2: search query db.test.find( { $text: { $search: "type a" } } )
if answer, c# code should easy.
Comments
Post a Comment