c#如何在mongodb对象中插入数组

本文关键字:插入 数组 对象 mongodb | 更新日期: 2023-09-27 18:13:19

目前我正在使用c#的应用程序,我想在其中添加一些数据到MongoDB集合。我试图将一个数组添加到一个员工对象,但我正在努力使它正确工作。

当我在看其他帖子时,我遇到了使用BsonDocument的语法,就像这样:

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

我想添加一个数组到函数属性,以添加描述和其他详细信息。

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

这是我现在的截图

这就是我想要完成的截图

以第二张截图为例,我想让'sensor_colle'为Function并添加一些特定的细节。

c#如何在mongodb对象中插入数组

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

public string[] Function { get; set; }

如果一个员工可以有多个职能,或者

public List<SomeFunctionObj> Function { get; set; 

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

看一下

Builders<YourModel>.Update.Push

如果您想要更新现有的项而不是插入它,则需要在Collection上使用Update方法而不是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"
    },
...
}