mongodb--c#驱动数据插入demo

本文介绍了如何使用C#驱动程序连接并插入数据到MongoDB数据库。首先从MongoDB官网或VS NuGet包管理器获取驱动,接着引入必要的命名空间。通过`MongoClient`实例连接本地MongoDB服务,创建数据库和集合对象。示例代码展示了如何定义一个BsonDocument,包括嵌套文档和数组,然后使用`InsertOneAsync()`方法将数据插入集合。最后,推荐使用Robomongo作为MongoDB的可视化管理工具来查看数据。
摘要由CSDN通过智能技术生成

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

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

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

这里我也把三个核心dll上传到了csdn方便下载;

http://download.csdn.net/detail/chenqiangdage/9288107

 

需要引入的命名空间:
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();
        }
    
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值