当前位置: 移动技术网 > IT编程>数据库>MongoDB > mongodb使用c#驱动数据插入demo

mongodb使用c#驱动数据插入demo

2019年05月28日  | 移动技术网IT编程  | 我要评论

mongodb提供了多种开发语言的驱动,java,python,c++,c# 等,这里选用c#驱动作为测试;

首先上mongo官网下载驱动。ps:官方网站经常连接不顺利。

还不如直接在vs的nuget管理包中搜索mongodb.driver.

需要引入的命名空间:

using mongodb.bson;
using mongodb.driver;

driver是驱动核心,bson是和数据格式相关的;

定义一个mongo客户端,一个mongodb,一个数据集合;

protected staticimongoclient client;
protected staticimongodatabase database;
protected staticimongocollection<bsondocument> collection;

连接上mongodb

//定义连接
client = new mongoclient("mongodb://127.0.0.1:27017");
//获取test数据库
database = client.getdatabase("test");     
//获取test数据库中的集合bios
collection = database.getcollection<bsondocument>("bios");

这里解释说明下:首先你得让mongod(mongo的服务端)运行起来,不然服务端都没开,怎么连接呢;目前测试还没有涉及到安全以及用户权限数据库管理这块,所以这里的连接都是使用的默认不带用户登录验证;

需求注意的是,如果我们建立的是控制台程序,那么这个连接必须写地址必须带端口,就像上面所写;

如果是建立的一个mvc web,你仅仅是测试数据插入,在这种无安全验证的方式下,你可以省去连接字符串。

如下图;

接下来就是定义一个测试数据:

var document =new bsondocument
      {
          { "address" , newbsondocument
            {
              { "street","2 avenue" },
              { "zipcode","10075" },
              { "building","1480" },
              { "coord",new bsonarray { 73.9557413, 40.7720266 } }
            }
          },
          { "borough", "manhattan"},
          { "cuisine", "italian"},
          { "grades", new bsonarray
              {
                new bsondocument
                {
                  { "date",new datetime(2014, 10, 1, 0, 0, 0, datetimekind.utc) },
                  { "grade","a" },
                  { "score",11 }
                },
                new bsondocument
                {
                  { "date",new datetime(2014, 1, 6, 0, 0, 0, datetimekind.utc) },
                  { "grade","b" },
                  { "score",17 }
                }
              }
          },
          { "name", "vella"},
          { "restaurant_id","41704620" }
      };

最后调用insertoneasync()方法;

collection.insertoneasync(document);

最终插入结果:

这里使用shell来看数据的话就太不直观了,这里使用的是比较常用的一个mongodb可视化管理工具robomongo 

附上代码:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using mongodb.bson;
using mongodb.driver;
namespace mongodbinsert
{
  class program
  {
    protected static imongoclient client;
    protected static imongodatabase database;
    protected static imongocollection<bsondocument> collection; 
    static void main(string[] args)
    {
       client = new mongoclient("mongodb://127.0.0.1:27017");
       database = client.getdatabase("test");
       collection = database.getcollection<bsondocument>("bios");
       for (int i = 0; i < 14; i++)
       {
         var document = new bsondocument
      {
          { "address" , new bsondocument
            {
              { "street", "2 avenue" },
              { "zipcode", "10075" },
              { "building", "1480" },
              { "coord", new bsonarray { 73.9557413, 40.7720266 } }
            }
          },
          { "borough", "manhattan" },
          { "cuisine", "italian" },
          { "grades", new bsonarray
              {
                new bsondocument
                {
                  { "date", new datetime(2014, 10, 1, 0, 0, 0, datetimekind.utc) },
                  { "grade", "a" },
                  { "score", 11 }
                },
                new bsondocument
                {
                  { "date", new datetime(2014, 1, 6, 0, 0, 0, datetimekind.utc) },
                  { "grade", "b" },
                  { "score", 17 }
                }
              }
          },
          { "name", "vella" },
          { "restaurant_id", "41704620" }
      };
         collection.insertoneasync(document);
       }
       console.writeline();
       console.readline();
    }
  }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网