I have this query which works when I run it in Robo 3T:
db.getCollection('groupSchedule').aggregate([{ "$match" : { "GroupId" : ObjectId("598dd346e5549706a80680bf") } },
{ "$lookup" : { "from" : "schedule", "localField" : "ScheduleIds", "foreignField" : "_id", "as" : "Schedule" } },
{ "$unwind" : "$Schedule" },
{ "$match" : {"$or": [{ "Schedule.End.ByDate" : {"$gte":new Date()}},{ "Schedule.End.ByDate" : null}] } },
{ "$group" : { "_id" : "$GroupId", "SurveyIds" : { "$addToSet" : "$Schedule.SurveyId" }, "ScheduleIds" : { "$addToSet" : "$Schedule._id" } } },
{ "$project" : { "_id" : 0, "SurveyIds" : 1, "ScheduleIds": 1 } }])
However, when I try to do the same thing using the C# driver as it blows up saying that:
"Duplicate element name 'Schedule.End.ByDate'.",
Here's the code:
return new List<BsonDocument>
{
Common.Util.MongoUtils.Match(new BsonDocument { { "GroupId", groupId } }),
Common.Util.MongoUtils.Lookup(scheduleCollections, "ScheduleIds", "_id", "Schedule"),
Common.Util.MongoUtils.Unwind("$Schedule"),
Common.Util.MongoUtils.Match(new BsonDocument
{
{
"$or", new BsonDocument
{
{
"Schedule.End.ByDate", BsonNull.Value
},
{
"Schedule.End.ByDate", new BsonDocument
{
{
"$gte", DateTime.UtcNow
}
}
}
}
}
}),
Group(),
Common.Util.MongoUtils.Project(new BsonDocument
{
{ "_id", 0 },
{ "SurveyIds", 1 },
{ "Schedules", 1 }
})
};
Any thoughts?