C # How to insert an array into a mongodb object

I am currently working on an application using C # where I want to add some data to the MongoDB collection. I'm trying to add an array to an Employee object, but I'm struggling to make it work correctly.

When I look at other posts, I come across a syntax using BsonDocument, something like this:

var document = new BsonDocument {
{ "author", "joe" },
{ "comments", new BsonArray {
    new BsonDocument { { "author", "jim" }, { "comment", "I disagree" } },
    new BsonDocument { { "author", "nancy" }, { "comment", "Good post" } }
}}

I want to add an array to the Function attribute to add a description and other detailed information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Core;

namespace Application
{
    class Program
    {
        public class Employee
        {
            public ObjectId Id { get; set; }
            public string BSN { get; set; }
            public string Name { get; set; }
            public string Function { get; set; 
        }

        static void Main(string[] args)
        {
            MongoClient client = new MongoClient();
            var server = client.GetServer();
            var db = server.GetDatabase("Database_Assignment");
            var collection = db.GetCollection<Employee>("Collection_Employees");

            Random random = new Random();

            List<string> names = new List<string>()        // Predefined list of names
            {
                "John Smith",
                "Matthew Williams",
                "David Harris",
                "Christopher Martin",
                "Paul Shark"
            };

            for (int i = 0; i < 5; i++)
            {
                int nameIndex      = random.Next(0, 5);         // Range in list of names
                int ageIndex       = random.Next(18, 67);       // Range for possible ages
                int funcIndex      = random.Next(0, 3);

                string nameValue   = names[nameIndex];         // Add index to string based on list of names
                string funcValue   = functions[funcIndex];     // Add index to string based on list of functions

                Employee employee = new Employee
                {
                    BSN = "BSN" + i,
                    Name = nameValue,
                    Function = funcValue
                };

                collection.Save(employee);
            }
        }
    }
}

Change 1

Here is what I screenshot now

Here is what I'm trying to take a screenshot

Taking the second screenshot, for example, I want the "sensor_colle" to be Function and add some specific data to it. Hope this helps.

+4
1

, , , , :

public string[] Function { get; set; }

,

public List<SomeFunctionObj> Function { get; set; 

, .

Builders<YourModel>.Update.Push

, , .

Builders<YourModel>.Update.AddToSet 

,

, , , , ,

, :

public class Employee
{
    public ObjectId Id { get; set; }
    public string BSN { get; set; }
    public string Name { get; set; }
    public EmployeeFunction Function { get; set; 
}

public class EmployeeFunction 
{
    public string FunctionDesc { get; set; }
    public string MoreInfo { get; set; }
}

, :

Employee employee = new Employee
            {
                BSN = "BSN" + i,
                Name = nameValue,
                Function = new EmployeeFunction 
                { 
                    FunctionDesc = funcValue, 
                    MoreInfo = "Some other info" 
                }
            };

, mongodb :

{
...
    "Function" : {
        "FunctionDesc" : "Developer",
        "MoreInfo" : "Some other info"
    },
...
}
+2

Source: https://habr.com/ru/post/1657979/


All Articles