Help with MongoDB .net driver
Hello, I recently join the community after starting a small project to get familiar with .net core and mongodb.
I'm trying to perform a simple unwind and replace root in .net core 2.2. I've already tried the query in MongoDB and it works but I'm finding it difficult to translate it 100 % to C# without using magic strings.
This is my document:
{ "_id" : ObjectId("5cb6475b20b49a5cec99eb89"), "name" : "Route A" "isActive" : true, "buses" : [ { "capacity" : "15", "time" : "08:00:00", "direction" : "Inbound" }, { "capacity" : "15", "time" : "08:30:00", "direction" : "Inbound" }, { "capacity" : "15", "time" : "08:00:00", "direction" : "Outbound" }, { "capacity" : "15", "time" : "08:30:00", "direction" : "Outbound" } ] }
I also have a class for the root document called Route and another one for the subdocument called Bus.
The query I'm running in mongo is this one:
db.routes.aggregate([ { $match : { "_id" : ObjectId("5cb4e818cb95b3572c8f0f2c") } }, { $unwind: "$buses" }, { $replaceRoot: { "newRoot": "$buses" } } ])
The expected result is a simple array of buses, so far I'm getting it with this query in C#:
_routes.Aggregate() .Match(r => r.Id == routeId) .Unwind(r => r.Buses) .ReplaceRoot("$buses") .ToListAsync();
I want to know if it's possible to replace the string "$buses" with something that's not a magic string.
I've tried using the AggregateExpressionDefinition class which is one of the possible parameters that the ReplaceRoot method can receive but I wasn't able to understand it completely.
Any help will be appreciated.
0 comments:
Post a Comment