As a beginner in software, I am trying to learn MongoDb, but there is a subject I need help with:
I want to see the license plate codes of the neighboring cities and the names of the cities in the information of the city I have selected in my project. For this, I need to use the .Match() and .Lookup() methods as a result of some research. When I try these results, I keep getting syntax errors. I am not sure if the structure I have set up is correct. Thank you for your help.
public class RelationalCityViewModel
{
[BsonId]
public ObjectId Id { get; set; }
public string PlakaCode { get; set; }
public string Name { get; set; }
public int OrderNo { get; set; }
public bool IsDelete { get; set; }
public int CountryId { get; set; }
public string Url { get; set; }
public List<NeighborCity> NeighborCities { get; set; }
}
public class RelationalCityCreateViewModel
{
public string PlakaCode { get; set; }
public string Name { get; set; }
public int OrderNo { get; set; }
public int CountryId { get; set; }
public string Url { get; set; }
public List<NeighborCity> NeighborCities { get; set; }
}
public class NeighborCity
{
[BsonId]
public ObjectId Id { get; set; }
public string PlakaCode { get; set; }
public string Name { get; set; }
public bool IsDelete { get; set; }
}
public async Task<BsonDocument> GetDataByIdWithFilter(FilterDefinition<BsonDocument>? filter, ObjectId id, RelationalCityViewModel relationalCityViewModel)
{
var result = _mongoDbCollection.Aggregate()
.Match(filter)
.Lookup(_mongoDbCollection,
relationalCityViewModel.NeighborCities.FirstOrDefault().Id,
relationalCityViewModel.Id,
(RelationalCityViewModel city, IEnumerable<RelationalCityViewModel> neighbors) => new
{
city.Name,
city.PlakaCode,
NeighborCities = neighbors.Select(n => new
{
n.Name,
n.PlakaCode
})
}
);
return result;
}
2