是否可以使用c#获取mongodb集合字段的数据类型?

时间:2015-03-21 01:33:34

标签: c# mongodb datatable

我需要使用字段的正确数据类型(String,double ...)创建一个存储mongodb查询结果的数据表。这里是我正在使用的代码但是在这里我将创建的列的类型指定为字符串类型我需要找到一种方法来获得正确的字段类型:

        var connectionString = "mongodb://localhost";
        var client = new MongoClient(connectionString);
        var server = client.GetServer();
        var database = server.GetDatabase("test");
        var collection = database.GetCollection("doc1"); 
        DataTable dt;
        DataColumn Column;
        int i = 0;

        dt = new DataTable();
        string map = @"function() { 
   for (var key in this) { emit(key, null); }
   }";
        string reduce = @"function(key, stuff) { return null; }";
        string finalize = @"function(key, value){
       return key;
   }";
        MapReduceArgs args = new MapReduceArgs();
        args.FinalizeFunction = new BsonJavaScript(finalize);
        args.MapFunction = new BsonJavaScript(map);
        args.ReduceFunction = new BsonJavaScript(reduce);
        var results = collection.MapReduce(args);
        List<string> fields = new List<string>();
        foreach (BsonValue result in results.GetResults().Select(item => item["_id"]))
        {
            fields.Add(result.AsString);
        }


        foreach (string p in fields)
        {// here I need to add the correct type of the field not string type
            Column = new DataColumn(p, Type.GetType("System.String"));
            dt.Columns.Add(Column);
            i++;
        }

0 个答案:

没有答案