下面的代码可用于查询从DataTable导入的mongo数据和通过MessageBox查看的输出。我已经验证它在匹配要查询的大写和小写字母的一些困难后工作
MongoClient mongo = new MongoClient("mongodb://localhost");
MongoServer server;
MongoDatabase database;
private void Form1_Load(object sender, EventArgs e)
{
server = mongo.GetServer();
server.Connect();
database = server.GetDatabase("test");
List<BsonDocument> batch = new List<BsonDocument>();
foreach (DataRow dr in dt.Rows)
{
var dictionary = dr.Table.Columns.Cast<DataColumn> ().ToDictionary(col => col.ColumnName, col => dr[col.ColumnName]);
batch.Add(new BsonDocument(dictionary));
}
MongoCollection<MongoDB.Bson.BsonDocument> collec = database.GetCollection<BsonDocument>("test");
collec.InsertBatch(batch); //// produces BsonIds for enteries
var results = batch.ToList();
string json = results.ToJson();
MessageBox.Show(json);
////Part I am struggling with
var query = new QueryDocument("Column1", "Henry");// PAY ATTENTION TO UPPER/LOWERCASE OF WORDS TO BE QUERIED
collec.Find(query).ToList(); // THIS PICKS CORRESPONDING RECORDS FROM THE MONGODB
}
答案 0 :(得分:0)
以上代码在核对查询项目中的大写/小写字母
之后有效