I am new to MongoDB and writing my first EntitySetController to access my sample data. My question is, is there an equivalent to navigation properties in MongoDB? I am trying to use an Include in my GET method, but without any luck. Here is my code so far :
Team object :
public class Team
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string TeamName { get; set; }
public string BadgeSmall { get; set; }
public string BadgeLarge { get; set; }
public string TeamImage { get; set; }
public string Formation { get; set; }
}
Fixture object :
public class Fixture
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public DateTime FixtureDate { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
//foreign key
public string AwayTeamId { get; set; }
//navigation properties
public virtual Team AwayTeam { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
//foreign key
public string HomeTeamId { get; set; }
//navigation properties
public virtual Team HomeTeam { get; set; }
public byte? AwayTeamScore { get; set; }
public byte? HomeTeamScore { get; set; }
public string AwayTeamScorers { get; set; }
public string HomeTeamScorers { get; set; }
}
Fixture controller :
[EnableQuery]
public IQueryable<Fixture> GetFixtures()
{
IQueryable<Fixture> m = mongoDatabase.GetCollection<Fixture>("Fixtures").FindAll().AsQueryable().Include("HomeTeam").Include("AwayTeam");
return m;
}
No, there is no notion of a navigation property. Because MongoDB doesn't support joins, it means we'd have to do at least 2 remote calls to hydrate your entity anyway. We'd rather you be explicit about it because there is a chance that you might simply model your data differently so that you can get it all with one query.
For instance, in your case it might be that you shouldn't only have the HomeTeamId and AwayTeamId in your Fixture, but also have some subset of denormalized data such that you don't need a second query.