私はMongoDBコレクションにテスト従業員データを追加する必要があるC#アプリケーションを開発しています。ユニークな郵便番号と国別ジェネレータを作成する
すでに動作中の接続があり、プログラムがループインしてデータを挿入する予定のリストがあります。しかし、私はアドレスを追加する必要があるポイントを追加するつもりです。
少なくとも10.000ユニークな郵便番号を作成するには、できるだけライブラリを使用せずにコードを作成する必要があります。
using System;
using System.Collections.Generic;
using MongoDB.Driver;
using MongoDB.Bson;
namespace Application
{
class Program
{
public class Employee
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Street { get; set; }
public string Country { get; set; }
public string Postalcode { get; set; }
}
static void Main(string[] args)
{
MongoClient client = new MongoClient();
var server = client.GetServer();
var db = server.GetDatabase("testdb");
var collection = db.GetCollection<Employee>("testcollection");
List<string> names = new List<string>()
{
"John Smith",
"Matthew Williams",
"David Harris",
"Christopher Martin",
"Paul Shark"
};
Random random = new Random();
for (int i = 0; i < 5; i++)
{
int nameIndex = random.Next(0, 5);
int projectIndex = random.Next(0, 3);
int ageIndex = random.Next(18, 67);
string nameValue = names[nameIndex];
Employee employee = new Employee
{
BSN = "BSN" + i,
Name = nameValue,
Age = ageIndex
};
collection.Save(employee);
}
}
}
}
私はこの他のページでコードを使用しようとしました:stackoverflow.com/questions/1344221/...しかし、郵便番号だけを作成します。郵便番号が実際にユニークであるかどうかは保証されませんので、私が探しているものではありません。 10.000の一意の郵便番号を作成する必要があります。すべての郵便番号が100%ユニークである場合、国/郵便番号の組み合わせも常に一意です。 - 編集された投稿 –