I’d like to save these classes in two separate collections, with the Categories
property containing just a link to the corresponding entry in the category collection.
public class Phonebook
{
public Guid Id {get; set;}
public string Name {get; set;}
public List<Category> Categories {get; set;}
}
public class Category
{
public Guid Id {get; set;}
public string Name {get; set;}
}
I’ve previously used LiteDb where I could do this to tell LiteDb that the Categories
is a reference.
BsonMapper.Global.Entity<PhoneBook>()
.DbRef(x => x.Categories, GetCollectionName<Category>());
and then access it using
database.GetCollection<Phonebook>()
.Include(x => x.Categories).FindOne(x => x.Id == myId);
(without the Include
it would just return the Id prop of the Categories
)
How does that translate to MongoDb and the C# driver?
I know from the sample data model you’d probably ask ‘why not include the in the Phonebook document?’, but the categories are used elsewhere and need to be managed separately (and the same categories need to be available throughout the system).