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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:16:00+00:00 2026-06-04T07:16:00+00:00

I am trying to insert nested documents in to a MongoDB using C#. I

  • 0

I am trying to insert nested documents in to a MongoDB using C#. I have a collection called categories. In that collection there must exist documents with 2 array, one named categories and one named standards. Inside those arrays must exist new documents with their own ID’s that also contain arrays of the same names listed above. Below is what I have so far but I am unsure how to proceed. If you look at the code what I want to do is add the “namingConventions” document nested under the categories array in the categories document however namingConventions must have a unique ID also.

At this point I am not sure I have done any of this the best way possible so I am open to any and all advice on this entire thing.

namespace ClassLibrary1
{
using MongoDB.Bson;
using MongoDB.Driver;
public class Class1
{
       public void test()
        {
            string connectionString = "mongodb://localhost";
            MongoServer server = MongoServer.Create(connectionString);
            MongoDatabase standards = server.GetDatabase("Standards");
            MongoCollection<BsonDocument> categories = standards.GetCollection<BsonDocument>("catagories");

            BsonDocument[] batch = {
                                       new BsonDocument { { "categories", new BsonArray {} },
                                                        { "standards", new BsonArray { } }  },
                                       new BsonDocument { { "catagories", new BsonArray { } },
                                                        { "standards", new BsonArray { } }  },
                                   };
            categories.InsertBatch(batch);

            ((BsonArray)batch[0]["categories"]).Add(batch[1]);
            categories.Save(batch[0]);           
        }
    }
}

For clarity this is what I need:

What I am doing is building a coding standards site. The company wants all the standards stored in MongoDB in a tree. Everything must have a unique ID so that on top of being queried as a tree it can be queried by itself also. An example could be:

/* 0 */
{
  "_id" : ObjectId("4fb39795b74861183c713807"),
  "catagories" : [],
  "standards" : []
}

/* 1 */
{
  "_id" : ObjectId("4fb39795b74861183c713806"),
  "categories" : [{
      "_id" : ObjectId("4fb39795b74861183c713807"),
      "catagories" : [],
      "standards" : []
    }],
  "standards" : []
}

Now I have written code to make this happen but the issue seems to be that when I add object “0” to the categories array in object “1” it is not making a reference but instead copying it. This will not due because if changes are made they will be made to the original object “0” so they will not be pushed to the copy being made in the categories array, at least that is what is happening to me. I hope this clears up what I am looking for.

  • 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-04T07:16:00+00:00Added an answer on June 4, 2026 at 7:16 am

    So, based on your latest comment, it seems as though this is the actual structure you are looking for:

    {
        _id: ObjectId(),
    
        name: "NamingConventions",
    
        categories: [
            {
                id: ObjectId(),
                name: "Namespaces",
                standards: [
                    {
                        id: ObjectId(),
                        name: "TitleCased",
                        description: "Namespaces must be Title Cased."
                    },
                    {
                        id: ObjectId().
                        name: "NoAbbreviations",
                        description: "Namespaces must not use abbreviations."
                    }
                ]
            },
            {
                id: ObjectId(),
                name: "Variables",
                standards: [
                    {
                        id: ObjectId(),
                        name: "CamelCased",
                        description: "variables must be camel cased."
                    }
                ]
            }
        ]
    }
    

    Assuming this is correct, then the below is how you would insert one of these:

    var collection = db.GetCollection("some collection name");
    
    var root = new BsonDocument();
    root.Add("name", "NamingConventions");
    var rootCategories = new BsonArray();
    rootCategories.Add(new BsonDocument
    {
       { "id": ObjectId.GenerateNewId() },
       { "name", "Namespaces" },
       { "standards", new BsonArray() }
    });
    
    root.Add("categories", rootCategories);
    //etc...
    collection.Save(root);
    

    Hope that helps, if not, I give up :).

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

Sidebar

Related Questions

I have code from my ad network that I am trying insert into an
im trying to insert an array values into the file using foreach.. and if
I am trying to insert a record into a table using Linq but get
I am trying to insert certain values into the table using SQL server 2005.
I'm trying to achieve some kind of nested transaction behavior using NHibernate's transaction control
I have this massive nested loop scenario that is calling the DB and making
Trying to insert values with Unicode Chars into a MySQL-database using Delphi 2010 and
Im trying to insert a keyspace into cassandra using hector and the SchemaManipulation example
I am trying to insert to date,time fields using a php script but I
I am trying to insert a new entity using LINQ-to-SQL, and entity is associated

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.