C#如何将数组插入对象mongodb

Jas*_*all 5 c# arrays mongodb

目前,我正在使用C#开发一个应用程序,在该应用程序中我想向MongoDB集合中添加一些数据。我正在尝试将数组添加到Employee对象,但是我正在努力使其正常工作。

当我查看其他帖子时,我遇到了使用BsonDocument的语法,如下所示:

var document = new BsonDocument {
{ "author", "joe" },
{ "comments", new BsonArray {
    new BsonDocument { { "author", "jim" }, { "comment", "I disagree" } },
    new BsonDocument { { "author", "nancy" }, { "comment", "Good post" } }
}}
Run Code Online (Sandbox Code Playgroud)

我想向Function属性添加数组以添加描述和其他详细信息。

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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑1

这是我目前拥有的屏幕截图

这就是我要完成的屏幕截图

以第二个屏幕截图为例,我希望'sensor_colle'为Function并向其中添加一些特定的细节。希望这可以帮助。

pie*_*eru 4

如果您希望 Function 成为一个数组,您应该将其更改为模型中的数组,因为它当前只是一个字符串属性,因此:

public string[] Function { get; set; }
Run Code Online (Sandbox Code Playgroud)

如果一名员工可以担任多项职能,或者

public List<SomeFunctionObj> Function { get; set; 
Run Code Online (Sandbox Code Playgroud)

如果它们可以有多个函数,并且每个函数有多个属性。

看一下

Builders<YourModel>.Update.Push
Run Code Online (Sandbox Code Playgroud)

如果您想要更新现有项目而不是插入它,则需要使用 Update 方法而不是 Save on the Collection。

或者

Builders<YourModel>.Update.AddToSet 
Run Code Online (Sandbox Code Playgroud)

如果您想将对象添加到数组中可能更合适

我希望这会有所帮助,如果我误解了您的问题,请告诉我,我会根据需要修改答案

更新

如果您想向函数添加更多信息,您将需要创建一个嵌入式模型:

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; }
}
Run Code Online (Sandbox Code Playgroud)

因此,要使用示例代码填充它:

Employee employee = new Employee
            {
                BSN = "BSN" + i,
                Name = nameValue,
                Function = new EmployeeFunction 
                { 
                    FunctionDesc = funcValue, 
                    MoreInfo = "Some other info" 
                }
            };
Run Code Online (Sandbox Code Playgroud)

这样,你的 mongodb 文档将如下所示:

{
...
    "Function" : {
        "FunctionDesc" : "Developer",
        "MoreInfo" : "Some other info"
    },
...
}
Run Code Online (Sandbox Code Playgroud)