MongoDB: query date by year C#
<p>Having such a document:</p>

<pre><code>var document = new BsonDocument
{
    { "address" , new BsonDocument
        {
            { "street", "2 Avenue" },
            { "zipcode", "10075" },
            { "building", "1480" },
            { "coord", new BsonArray { 73.9557413, 40.7720266 } }
        }
    },
    { "borough", "Manhattan" },
    { "cuisine", "Italian" },
    { "grades", new BsonArray
        {
            new BsonDocument
            {
                { "date", new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) },
                { "grade", "A" },
                { "score", 11 }
            },
            new BsonDocument
            {
                { "date", new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) },
                { "grade", "B" },
                { "score", 17 }
            }
        }
    },
    { "name", "Vella" },
    { "restaurant_id", "41704620" }
};
</code></pre>

<p>How would I query for grades.date.year = 2016?</p>

<p>Was trying:</p>

<pre><code>var filter = Builders&lt;BsonDocument&gt;.Filter.Eq("grades.date.year", 2016);
var result = await collection.Find(filter).ToListAsync();
</code></pre>

<p>But I guess dot notation only works on the json doc, not the objects? Scoured the internet, but couldn't find a clean example.</p>

<p>EDIT: C# classes?</p>

<pre><code>class Address
{
    public string street { get; set; }
    public string zipcode { get; set; }
    public string building { get; set; }
    public IEnumerable&lt;double&gt; coord { get; set; }
}

class Grade
{
    public DateTime date { get; set; }
    public string grade { get; set; }
    public int score { get; set; }
}

class TestX
{
    public ObjectId _id { get; set; }
    public Address address { get; set; }
    public string borough { get; set; }
    public string cuisine { get; set; }
    public IEnumerable&lt;Grade&gt; grades { get; set; }
    public string name { get; set; }
    public string restaurant_id { get; set; }
}
</code></pre>