Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6766807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:52:31+00:00 2026-05-26T14:52:31+00:00

I have come across a situation where it would be better to represent a

  • 0

I have come across a situation where it would be better to represent a particular part of my domain model as relational. I read the section on database references in MongoDB and understand you can provide references to multiple documents by providing a JSON array of $ref references to various documents in a foreign collection.

All the examples I’ve seen for adding a reference to a foreign document in code have been only for a single document and they’ve created a public property of type MongoDBRef. There is a lot of unnecessary overhead with this approach, in my opinion, but it also doesn’t make it clear what to do about storing references to multiple documents.

If you would like to provide a one-to-many relationship between foreign documents in Mongo, is it necessary to provide a collection property containing MongoDBRef objects? Is it possible to stick to a collection of standard entity objects in my C# code and map it to Mongo documents using BsonClassMap?

Below is a simple class that represents the model I currently have. It seems to be saving the document and references correctly, but I don’t like exposing a public collection of MongoDBRef objects and the overhead it takes to add new documents for anybody that uses the Person class.

If it matters, I’m using MongoDB 2.0 and their C# driver.

// This is how my class currently looks
public class Person
{
    public string Name { get; set; }
    public List<MongoDBRef> Vehicles { get; private set; }

    public Person()
    {
        Vehicles = new List<MongoDBRef>();
    }
}

// This is what I want my class to look like
public class Person
{
    public string Name { get; set; }
    public List<Vehicle> Vehicles { get; private set; }

    public Person()
    {
        Vehicles = new List<Vehicle>();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:52:32+00:00Added an answer on May 26, 2026 at 2:52 pm

    DBRefs are not the appropriate tool for storing references to a known document type. Instead just save the _id values of the referred documents in your collection. Given a good mapping library (not sure about C# but the C# equivalent of pymongo, mongoose, morphia, etc.) it will allow you to do exactly what you want.

    DBRefs should only be used if you do not know at compile time what kind of document you need to store a reference to (e.g. a “content” field that either holds an Image or a Text, etc.).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across an awkward situation where I would like to have a
I have come across a situation (which I think is weird but is possibly
I have recently come across a situation where code is dynamically loading some libraries,
I have come across this situation. In the hash1 first column is the key
I have come across a situation I have not encountered before using the IF
I have come across a situation where Tasks I am creating only seem to
I have come across a rather odd situation. I have generated a a random
I have come across a situation that conflicts with my current understanding of methods
I've recently come across the following situation. I have a UserControl that can show
I have come across a situation today which has me wondering about best practices.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.