栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 数据库 > MongoDB

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

MongoDB 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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 collection;

连接上MongoDB

//定义连接
client = new MongoClient("mongodb://127.0.0.1:27017");
//获取test数据库
database = client.GetDatabase("test");     
//获取test数据库中的集合bios
collection = database.GetCollection("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 collection; 
    static void Main(string[] args)
    {
client = new MongoClient("mongodb://127.0.0.1:27017");
database = client.GetDatabase("test");
collection = database.GetCollection("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();
    }
  }
}

总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/172884.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号