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 9241241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:20:10+00:00 2026-06-18T08:20:10+00:00

When I have a root document that has a list of documents and I

  • 0

When I have a root document that has a list of documents and I save/insert, the root document always gets an ID from MongoDB, but the documents in the list don’t, they remain the same as ObjectId.Empty. I’m not sure if I’m doing something wrong or if this is intended behavior?

Here’s an example of what I’m trying to do:
Given the classes:

public class Foo
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
    public IList<Bar> Bars { get; set; }
}

public class Bar
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
}

With a Factory:

public class Factory
{
    public Foo CreateFoo(string name)
    {
        return new Foo {
            Id = new ObjectId(),
            Name = name,
            Bars = new List<Bar>()
        };
    }

    public Bar CreateBar(string name)
    {
        return new Bar {
            Id = new ObjectId(),
            Name = name
        };
    }
}

When I run a sample like this:

public class Program
{
    static void Main(string[] args)
    {
        var client = new MongoClient("mongodb://localhost");
        var server = client.GetServer();
        var database = server.GetDatabase("Example1");
        if (!database.CollectionExists("foos")) database.CreateCollection("foos");

        var factory = new Factory();
        var foo1 = factory.CreateFoo("first");
        foo1.Bars.Add(factory.CreateBar("bar"));
        var collection = database.GetCollection("foos");
        collection.Insert(foo1);

        var foo2 = factory.CreateFoo("second");
        collection.Insert(foo2);
        foo2.Bars.Add(factory.CreateBar("bar"));
        collection.Save(foo2);
    }
}

Both foo1 and foo2 get _id‘s generated by MongoDB, but none of the Bars do. All the Bars still have the ObjectId.Empty. Now an easy solution is to update the factory to use ObjectId.GenerateNewId(), but I’d feel more comfortable with it being generated from the DB. Does anyone have any insight on this? Am I doing something wrong? Is this even possible? Thanks for the input.

  • 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-06-18T08:20:11+00:00Added an answer on June 18, 2026 at 8:20 am

    The driver will only generate the Id field for you when the fields is identified as the _id of a collection. In your case, Bar is embedded inside the Foo document.

    Therefore, it doesn’t make sense for Bar to have an auto-generated Id field.

    In your example, there is really no reason for Bar to have a unique _id. When you query the collection, you will query based on the _id of Foo and you’ll have Bar objects available in the response.

    This is definitely intended behavior.

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

Sidebar

Related Questions

I have a root element in my output xml document that has no attributes:
I have a document that has the following format: <Root> <A /> <C />
I have a document that looks something like <root> <element> <subelement1 /> <subelement2 />
I have a xml document that has a record set like this. <document> <row>
All, I have an XML document that looks something like this: <root> <profile> <childA>
I have a content type that has a lookup field for a document library
I have used the following code to get the document root. $path = get_file_dir();
I have an XML document with a default namespace indicated at the root. Something
I have a document, something like this: <root> <A node=1/> <B node=2/> <A node=3/>
Suppose I have an XML document stored as an Anti-XML Elem : val root

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.