C#如何将数组插入对象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" } }
}}

我想在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);
            }
        }
    }
}

编辑1

这就是我目前拥有的screenshot

这就是我想要完成的工作screenshot

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

最佳答案 如果你想让Function成为一个数组,你应该将它改为模型中的数组,因为它目前只是一个字符串属性,所以:

public string[] Function { get; set; }

如果员工可以拥有多个功能,或者

public List<SomeFunctionObj> Function { get; set; 

如果它们可以具有多个功能,并且每个功能都具有多个属性.

看一下

Builders<YourModel>.Update.Push

如果要更新现有项而不是插入项,则需要使用Update方法而不是Collection上的Save.

另外

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"
    },
...
}
点赞