MongoDB + C# driver + query array of elements where each array element contains sub-document to query on

前端 未结 1 1947
广开言路
广开言路 2020-12-10 17:00

I\'m using version 1.5.0.4566 of the official MongoDB C# driver. I\'m using Mongo version 2.06.

Here is what my document structure looks like (omitted fields not n

相关标签:
1条回答
  • 2020-12-10 17:19

    Try this instead

    Query.ElemMatch("Children", Query.And(Query.EQ("StatusId",1), Query.EQ("Active",true),Query.LT("SubChild.ExpiresOn",DateTime.UtcNow)));
    

    Wondering why this query magically works? It's the case (StatusId vs StatusID). JavaScript is case sensitive.

    You could eliminate this problem by using strongly typed Linq queries, like:

    from x in collection.AsQueryable()
    where x.Children.Any(child => 
        child.StatusId == 1 
        && child.Active 
        && child.SubChild.ExpiresOn < DateTime.UtcNow)
    select x
    
    0 讨论(0)
提交回复
热议问题