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(); } } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 不止黑神话!杭州给原创企业真金白银扶持 打造“杭州游戏”品牌
- 很有多样性:《战锤》首次引入非二元性别角色
- 《少女前线2:追放》项目组将裁员 官方:进一步优化组织结构
- 陈慧琳.2002-闪亮每一天新歌+精选2CD【正东】【WAV+CUE】
- 袁培华.1992-有缘【歌林】【WAV+CUE】
- 王瑞霞.1996-是我太软心【名冠】【WAV+CUE】
- 《谭维维 30首经典流行歌曲精选辑 2CD》[WAV/分轨][1.1GB]
- 《赵传 出道三十年全新大碟 你过得还好吗》[WAV/分轨][460MB]
- 《发烧四大天王 磁性男声HiFi发烧碟 2CD》[WAV/分轨][1GB]
- 黑神话悟空上品鼠司空精魄获取方法一览|上品鼠司空精魄收集攻略
- 《真三国无双:起源》新情报将于9月26日公开,实机演示亮相!
- 黑神话悟空上品鼠禁卫精魄获取方法一览|上品鼠禁卫精魄收集攻略
- 优美的旋律至真的情怀《品茶轻音乐·极品典藏Vol.1CD1》[WAV+CUE]
- 曾嵘《峥嵘岁月DSD(试音碟)》WAV+CUE
- 小提琴天碟AlfredoCampoli坎波利-贝多芬D大调小提琴协奏曲与普罗米修斯作品选FLAC