c# - ElemMatch in MondoDb (C #, driver 2.0) -


trying query find records in field (which array) contains specified string:

var builder = builders<productmodeldb>.filter; var eqfilter = builders<string>.filter.eq(x => x, "\nhome"); var matchfilter = builder.elemmatch<string>(x=>x.productdata.breadcams, eqfilter); var res = productcollection.find(matchfilter).tolistasync().result; 

data model:

public class productmodeldb {     [bsonid]     public string url;     public datetime datetime;     public productdatamodel productdata; }  public class productdatamodel    {             public list<string> title { get; set; }     public list<string> breadcams { get; set; } } 

when start program receive error:

system.argumentnullexception: value cannot null. parameter name: fieldname @ mongodb.driver.core.misc.ensure.isnotnull[string] (system.string value, system.string paramname) [0x00000] in <filename unknown>:0  @ mongodb.driver.renderedfielddefinition`1[system.string]..ctor (system.string fieldname, ibsonserializer`1 fieldserializer) [0x00000] in <filename unknown>:0  @ mongodb.driver.expressionfielddefinition`2[system.string,system.string].render (ibsonserializer`1 documentserializer, ibsonserializerregistry serializerregistry) [0x00000] in <filename unknown>:0  @ mongodb.driver.simplefilterdefinition`2[system.string,system.string].render (ibsonserializer`1 documentserializer, ibsonserializerregistry serializerregistry) [0x00000] in <filename unknown>:0  @ mongodb.driver.elementmatchfilterdefinition`2[datamodels.productmodeldb,system.string].render (ibsonserializer`1 documentserializer, ibsonserializerregistry serializerregistry) [0x00000] in <filename unknown>:0  @ mongodb.driver.mongocollectionimpl`1[datamodels.productmodeldb].findasync[productmodeldb] (mongodb.driver.filterdefinition`1 filter, mongodb.driver.findoptions`2 options, cancellationtoken cancellationtoken) [0x00000] in <filename unknown>:0  @ mongodb.driver.findfluent`2[datamodels.productmodeldb,datamodels.productmodeldb].tocursorasync (cancellationtoken cancellationtoken) [0x00000] in <filename unknown>:0  @ mongodb.driver.iasynccursorsourceextensions+<tolistasync>d__14`1[datamodels.productmodeldb].movenext () [0x00000] in <filename unknown>:0  --- end of inner exception stack trace --- @ system.threading.tasks.task`1[system.collections.generic.list`1[datamodels.productmodeldb]].get_result () [0x00000] in <filename unknown>:0  @ databaseprocesser.categoryprocessor.createcategory () [0x00000] in <filename unknown>:0  @ databaseprocesser.program.main (system.string[] args) [0x00000] in <filename unknown>:0  --> (inner exception 0) system.argumentnullexception: value cannot null. 

what error is? how write queries elemmatch?

your query doesn't make sense. $elemmatch used if match multiple criteria each list item, e.g. when finding element following votes user's name john and voted protoss.

{     "_id" : "foo",    "votes" : [ { "user" : "john", "for" : "protoss" },                { "user" : "mike", "for" : "zerg" }, ... ] } 

for list of non-objects, i.e. list of strings in example, can use eq query if field no list in first place. example, on console:

db.products.find({"productdata.breadcams" : "home"}); 

p.s.: mean breadcrumbs?


Comments