3

I have 2 classes:

public class Store
{
   public Guid ID;
   public List<Product> Products;
}

public class Product
{
   public Guid ID;
   public string Name;
}

I want to save "Store" in my MongoDB but I don't want the collection of Stores to include the content of "Product" (to save space \ duplication). I want to create another colleciton of Product, and use some kind of ID reference to the matched document.

Does MongoDB 2.4.4 c# driver support this without changing my models? (I can't modify them - used in an API calls). How can I implement it?

Aviram Fireberger
  • 3,910
  • 5
  • 50
  • 69
  • Please do not use `DBRef`. It simply is not supported in many operations. For example [`$lookup`](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/) which is a core feature and not driver specific. – Neil Lunn Jun 18 '17 at 13:35
  • @NeilLunn Thanks for the comment, So which reference object can I use? – Aviram Fireberger Jun 19 '17 at 06:21
  • You don't. Generally speaking "relationships" are really domain logic to be defined in your data access layer. There are many ways to do that and opinions greatly differ. You should however look at ["Manual References"](https://docs.mongodb.com/manual/reference/database-references/#manual-references) in the core documentation as well as the `$lookup` operator link I already gave you. This should give you enough to gain an understanding. There is also a [`$lookup`](http://mongodb.github.io/mongo-csharp-driver/2.2/reference/driver/crud/linq/#lookup) example in the LINQ guide. – Neil Lunn Jun 19 '17 at 06:32

1 Answers1

0

Using [BsonIgnore] would do the trick if I understand you right.

MongoDB C# Driver: Ignore Property on Insert

Janolof.
  • 141
  • 8