2016-10-17 14 views
4

現在、私はMongoDBコレクションにいくつかのデータを追加するC#を使用してアプリケーションで作業しています。私はEmployeeオブジェクトに配列を追加しようとしていますが、正しく動作させるためには苦労しています。C#私はオブジェクトのmongodbに配列を挿入する方法

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

私が説明し、その他の詳細情報を追加する属性を関数に配列を追加したい:私は他の記事で探していたとき

、私はBsonDocument、このようなものを使用した構文に遭遇します。

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

を持っているもの。これは、私が示すように第2のスクリーンショットを撮るscreenshot

を達成しようとしているものですが、私は「sensor_colleをしたいです'をFunctionにして、それに具体的な詳細を追加します。お役に立てれば。

答えて

2

あなたは、それはそう、現在だけで文字列プロパティですとしての機能は、あなたがあなたのモデルのものにそれを変更する必要があり、配列にしたい場合は、次の従業員が複数の機能を持たせることができる

public string[] Function { get; set; } 

場合、または

public List<SomeFunctionObj> Function { get; set; 

複数の機能を持つことができ、各機能に複数のプロパティがある場合。

は、あなたがあなたの代わりにそれを挿入する既存のアイテムを更新するために欠けている場合はコレクションに代わりに保存のUpdateメソッドを使用する必要があります

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

:あなたが埋め込まれたモデルを作成する必要があります機能にmation

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" 
    }, 
... 
} 
+0

お返事ありがとうございます。私のプログラムは毎回更新する必要はなく、挿入するだけで十分です。元の投稿を編集して、私が達成しようとしていることを説明しました。 –

+1

埋め込みモデルの解説を更新しました。これは、Functionプロパティの下にさらに情報を保存する方法です。これはnosqlの美しさの1つです。伝統的にRDBMSでは、このために全く新しいテーブルが必要で、Employeeエンティティに対する外部キーを格納します。 MongoDbを使うと、埋め込むことができます。埋め込みの対象とするかどうかを決定するときに最も重要な質問の1つは、「頻繁に埋め込みデータが変更される頻度」です。頻繁に変更される場合は、従来の参照が最もよいでしょうが、埋め込みがうまくいくと感じます。 – pieperu

+1

これは**私が必要としていたものです**。それは完璧に働いた。どうもありがとうございました! –

関連する問題