Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested document insert into MongoDB with C#

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.

like image 263
John Stewart Avatar asked Feb 20 '23 12:02

John Stewart


1 Answers

So, I guess I'm confused by what you are asking. If you just want to store the namingConventions documents inside the array, you don't need a collection for them. Instead, just add them to the bson array and store them.

var categoriesCollection = db.GetCollection<BsonDocument>("categories");

var category = new BsonDocument();
var namingConventions = new BsonArray();
namingConventions.Add(new BsonDocument("convention1", "value"));

category.Add("naming_conventions", namingConventions);

categoriesCollection.Insert(category);

This will create a new document for a category, create an array in it called naming_conventions with a single document in it with an element called "convention1" and a value of "value".

like image 153
Craig Wilson Avatar answered Mar 06 '23 05:03

Craig Wilson