如何使用字段值/内容查询List <bsondocument>批处理Mongo数据库

时间:2017-07-10 23:04:31

标签: mongodb datatable mongodb-query

下面的代码可用于查询从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 




         }

1 个答案:

答案 0 :(得分:0)

以上代码在核对查询项目中的大写/小写字母

之后有效