Nested document insertion in MongoDB c #

I am trying to insert nested documents into MongoDB using C #. I have a collection called categories. This collection should contain documents with 2 arrays, one of the named categories and one named standard. Inside these arrays, new documents must exist with their identifier, which also contain arrays with the same names as above. Below is what I have so far, but I'm not sure how to proceed. If you look at the code, what I want to do is add the “namingConventions” document nested in the categories array in the categories document, however the naming names must also have a unique identifier.

At this moment, I’m not sure that I did all this in the best way, therefore I am open to all and any advice on all this.

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 creating a coding standards site. The company wants all standards to be stored in MongoDB in a tree. Everything must have a unique identifier so that, in addition to being requested as a tree, it can also be requested by itself. An example would be:

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

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

, , , "0" "1", , . , , , "0", , , , , . , , .

+3
4

, , , , :

{
    _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."
                }
            ]
        }
    ]
}

, , , :

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);

, , , :).

+6

, , , . namingConventions , . bson .

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);

, naming_conventions , "1", "value".

0

, . , JSON, # , .

, , JSON, #, , , #.

One thing that doesn't sound right in your original description is the statement "there must be 2 arrays in this collection." A collection can only contain documents, not arrays. The documents themselves can contain arrays if you want.

0
source
var filter = Builders<CollectionDefination>.Filter.Where(r => r._id== id);
var data = Builders<CollectionDefination>.Update.Push(f=> 
    f.categories,categoriesObject);
await _dbService.collection.UpdateOneAsync( filter,data);

Note. Make sure that the type of the embedded document [Categories] is an array.

0
source

All Articles