Using $and in the pipeline for mongodb aggregate() function driver in C# -
Using $and in the pipeline for mongodb aggregate() function driver in C# -
i trying work mongodb aggregation framework in c#.
i want able set $and in query. here's mongodb query want run-
db.students.aggregate( { $match: { name:"mira", $and:[{date:{$gte:isodate("2015-03-01t00:00:00")}}, {date:{$lte:isodate("2015-04-01t00:00:00")}}] }, { $group:{"_id":"$subject", "$sum":"$marks"} } )
i created match corresponding mongodb query isn't right here because compiler errors on brackets. c# code follows-
var match = new bsondocument { { "$match", new bsondocument { { "name", "mira" } }, { "$and", new bsondocument{ { new bsondocument { { "date", new bsondocument { { "$gte", begindate.tostring("yyyy-mm-ddthh:mm:ss") } } } }, new bsondocument { { "date", new bsondocument { { "$lte", enddate.tostring("yyyy-mm-ddthh:mm:ss") } } } } }} } } } ;
can guide me towards how set $and in match in c# code?
you this:
var match= new bsondocument("$match", query.and(query.eq("name", "mira"), query.gte("date", begindate.tostring("yyyy-mm-ddthh:mm:ss")), query.lte("date", enddate.tostring("yyyy-mm-ddthh:mm:ss"))).tobsondocument());
or follow variant recommended @chridam:
var match = new bsondocument { { "$match", new bsondocument { { "name", "mira" }, { "date", new bsondocument { {"$gte", begindate.tostring("yyyy-mm-ddthh:mm:ss")}, {"$lte", enddate.tostring("yyyy-mm-ddthh:mm:ss") } } } } } };
c# mongodb aggregate
Comments
Post a Comment