Reputation: 7726
Having such a document:
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" }
};
How would I query for grades.date.year = 2016?
Was trying:
var filter = Builders<BsonDocument>.Filter.Eq("grades.date.year", 2016);
var result = await collection.Find(filter).ToListAsync();
But I guess dot notation only works on the json doc, not the objects? Scoured the internet, but couldn't find a clean example.
EDIT: C# classes?
class Address
{
public string street { get; set; }
public string zipcode { get; set; }
public string building { get; set; }
public IEnumerable<double> 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<Grade> grades { get; set; }
public string name { get; set; }
public string restaurant_id { get; set; }
}
Upvotes: 0
Views: 1162
Reputation: 9473
This requires aggregation framework. If you could post c# class then will update my answer to strongly typed c# , but now decided to project year inside project phase.
public static void Main()
{
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("test");
var collection = database.GetCollection<BsonDocument>("hammer");
var project =
BsonDocument.Parse(
"{_id: 1,address: 1,borough: 1,cuisine: 1,grades: 1,name: 1,restaurant_id: 1,year: {$year: '$grades.date'}}");
var aggregationDocument =
collection.Aggregate()
.Unwind("grades")
.Project(project)
.Match(BsonDocument.Parse("{'year' : {$in : [2013, 2015]}}"))
.ToList();
foreach (var result in aggregationDocument)
{
Console.WriteLine(result.ToString());
}
Console.ReadLine();
}
var aggregationDocument =
collection.Aggregate<TestX>()
.Unwind<TestX>(x=>x.grades)
.Match(BsonDocument.Parse(
"{$and:[{'grades.date':{$gte: ISODate('2012-01-01')}},{'grades.date':{$lt: ISODate('2013-01-01')}}]}"))
.ToList();
Upvotes: 2
Reputation: 2275
Couldn't another approach be to check if date is within that year.
I.e for 2016 using $gte: 2016-01-01 00:00:00
and $lt: 2017-01-01 00:00:00
Find objects between two dates MongoDB
There are probably functions for them in c# like Gte
and Lt
. There are probably some easy way to combine the filters like with an &
or similar.
Upvotes: -1