+driver+tutorial 笔记 首先下载驱动。驱动有两个文件 mongodb.bson.dll mongodb.driver.dll 可以直接下载这两个驱动,或者按照下载源码进行编译生成。下载的源码可以看些test例子。 在新建的c#工程中添加这两个dll文件,并且使用如下命名空间 至少要引用如
+driver+tutorial
笔记
首先下载驱动。驱动有两个文件
mongodb.bson.dllmongodb.driver.dll可以直接下载这两个驱动,或者按照下载源码进行编译生成。下载的源码可以看些test例子。
在新建的c#工程中添加这两个dll文件,并且使用如下命名空间
至少要引用如下命名空间
using mongodb.bson;using mongodb.driver; 另外使用比较多的命名空间是using mongodb.driver.builders;using mongodb.driver.gridfs;using mongodb.driver.linq;
另外有些可能会用得到的命名空间
using mongodb.bson.io;using mongodb.bson.serialization;using mongodb.bson.serialization.attributes;using mongodb.bson.serialization.conventions;using mongodb.bson.serialization.idgenerators;using mongodb.bson.serialization.options;using mongodb.bson.serialization.serializers;using mongodb.driver.wrappers;bson类库bson是类似json的一种二进制形式的存储格式,简称binary json,它和json一样,支持内嵌的文档对象和数组对象,但是bson有json没有的一些数据类型,如date和bindata类型。它也是mongodb文档数据库内部的数据存储方式。bsontype public enum bsontype {double = 0x01,string = 0x02,document = 0x03,array = 0x04,binary = 0x05,undefined = 0x06,objectid = 0x07,boolean = 0x08,datetime = 0x09,null = 0x0a,regularexpression = 0x0b,javascript = 0x0d,symbol = 0x0e,javascriptwithscope = 0x0f,int32 = 0x10,timestamp = 0x11,int64 = 0x12,minkey = 0xff,maxkey = 0x7f}bsonvalue和子类
bsonvalue是一种代表bsontype的虚拟类。它是bsontype枚举类的凝聚子类。
·可以使用public构造函数生成bsonvalue子类
·使用静态create函数生成
·use a static property of a subclass of bsonvalue(静态的子类属性?)
·隐式转换成bsonvalue
bsontype的类型
可以用下面的例子代码确认bsonvalue的属性
bsonvalue value;if (value.bsontype == bsontype.int32) {// we know value is an instance of bsonint32}if (value is bsonint32) {// another way to tell that value is a bsonint32}if (value.isint32) {// the easiest way to tell that value is a bsonint32}as[type] properties
bsonvalue有一系列转换方式将它的类型cast(抛)(而不是conversion)成与.net相匹配的数据类型。如果他不是一个.net相对应的数据属性,它将会抛出一个invalidcastexception 异常。下面是一些将数据转变的方式。
bsondocument document;string name = document[name].asstring;//as方式,类似转变int age = document[age].asint32;bsondocument address = document[address].asbsondocument;string zip = address[zip].asstring;is[type] properties
使用下面例子测试bsonvalues是什么类型
bsondocument document;int age = -1;if (document.contains[age] && document[age].isint32) {//is 是否为int32类型age = document[age].asint32;}to[type] 转变方法与as不同,to是用于可以转变类型之间的转类型。比如int和double之间。比如toboolen方法永远不会失败。它是按照javascript里面定义的。false, 0, 0.0, nan, bsonnull, bsonundefined 以及 是false,其他所有都是true。if (employee[ismanager].toboolean()) {// we know the employee is a manager// works with many ways of recording boolean values}todouble、toint32、以及toint64在数字之间的转变都不会失败。即使数字长度不匹配被缩短了都不会照成函数错误。string类型可以转成数字类型。但是如果string类型不能转成相应的数字的时候,会抛出异常。隐式的转化下面的数据类型可以直接转化比如下面
bsonvalue b = true; // b is an instance of bsonbooleanbsonvalue d = 3.14159; // d is an instance of bsondoublebsonvalue i = 1; // i is an instance of bsonint32bsonvalue s = hello; // s is an instance of bsonstringbsonmaxkey, bsonminkey, bsonnull and bsonundefined
这些数据类型是单个的类,要用到这些数据,需要使用各自的类来生成
document[status] = bsonnull.value;document[priority] = bsonmaxkey.value;注意,这个c#的null和bsonnull是两个完全不同的东西。bsonnull是一个c#类,它的value属性是null。所以他们在函数构造不同。 objectid and bsonobjectid一些常用的创建objectid 值的方式var id1 = new objectid(); // same as objectid.emptyvar id2 = objectid.empty; // all zeroesvar id3 = objectid.generatenewid(); // generates new unique idvar id4 = objectid.parse(4dad901291c2949e7a5b6aa8); // parses a 24 hex digit string在c#里面,美国空间,刚创建的值默认都是零的。但是在javascript里面会创建一个唯一的值。
bsonelement
(bson元素)bson元素是一个name/value的键值对。document.add(new bsonelement(age, 21)); // ok, but next line is shorterdocument.add(age, 21); // creates bsonelement automaticallybsondocument
bsondocument是name/value键值对的集合。bsondocument构造函数bsondocument()bsondocument(string name, bsonvalue value)上面是用的比较多