MongoDb中的ElemMatch(C#,驱动程序2.0)

时间:2015-05-08 05:52:07

标签: c# mongodb

尝试进行查询以查找字段(数组)包含指定字符串的记录:

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;

数据模型:

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; }
}

启动程序时,我收到此错误:

System.ArgumentNullException: Value cannot be null.
Parameter name: fieldName
at MongoDB.Driver.Core.Misc.Ensure.IsNotNull[String] (System.String value, System.String paramName) [0x00000] in <filename unknown>:0 
at MongoDB.Driver.RenderedFieldDefinition`1[System.String]..ctor (System.String fieldName, IBsonSerializer`1 fieldSerializer) [0x00000] in <filename unknown>:0 
at MongoDB.Driver.ExpressionFieldDefinition`2[System.String,System.String].Render (IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry) [0x00000] in <filename unknown>:0 
at MongoDB.Driver.SimpleFilterDefinition`2[System.String,System.String].Render (IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry) [0x00000] in <filename unknown>:0 
at MongoDB.Driver.ElementMatchFilterDefinition`2[DataModels.ProductModelDb,System.String].Render (IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry) [0x00000] in <filename unknown>:0 
at 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 
at MongoDB.Driver.FindFluent`2[DataModels.ProductModelDb,DataModels.ProductModelDb].ToCursorAsync (CancellationToken cancellationToken) [0x00000] in <filename unknown>:0 
at MongoDB.Driver.IAsyncCursorSourceExtensions+<ToListAsync>d__14`1[DataModels.ProductModelDb].MoveNext () [0x00000] in <filename unknown>:0 
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1[System.Collections.Generic.List`1[DataModels.ProductModelDb]].get_Result () [0x00000] in <filename unknown>:0 
at DataBaseProcesser.CategoryProcessor.CreateCategory () [0x00000] in <filename unknown>:0 
at DataBaseProcesser.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 
--> (Inner exception 0) System.ArgumentNullException: Value cannot be null.

我的错误是什么?如何编写查询ElemMatch?

1 个答案:

答案 0 :(得分:0)

您的查询没有意义。仅当您为每个列表项匹配多个条件时才使用$elemMatch,例如当从以下votes中找到用户姓名为john 的元素时,他投票支持Protoss。

{ 
   "_id" : "foo",
   "votes" : [ { "user" : "john", "for" : "Protoss" },
               { "user" : "mike", "for" : "Zerg" }, ... ]
}

对于非对象列表,即示例中的字符串列表,您可以简单地使用eq查询,就好像该字段首先没有列表一样。例如,在控制台上:

db.products.find({"productData.breadcams" : "Home"});

p.s:你的意思是breadcrumbs