我正在开发一个C#应用程序,我必须将测试员工数据添加到MongoDB集合中。创建独特的邮政编码和国家/地区生成器
已经有一个工作连接和一些预定义列表,程序将循环插入数据。但是,我卡住了,我必须添加地址。
我需要一段代码才能创建至少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%唯一的,那么国家/邮政编码的组合也是唯一的。 - 编辑帖子 –