Home Database Mysql Tutorial MongoDB C#驱动

MongoDB C#驱动

Jun 07, 2016 pm 05:56 PM
mongodb drive

+Driver+Tutorial 笔记 首先下载驱动。驱动有两个文件 MongoDB.Bson.dll MongoDB.Driver.dll 可以直接下载这两个驱动,或者按照下载源码进行编译生成。下载的源码可以看些test例子。 在新建的c#工程中添加这两个dll文件,并且使用如下命名空间 至少要引用如

+Driver+Tutorial

笔记

首先下载驱动。驱动有两个文件

  • MongoDB.Bson.dll
  • MongoDB.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 BsonBoolean BsonValue d = 3.14159; // d is an instance of BsonDouble BsonValue i = 1; // i is an instance of BsonInt32 BsonValue s = "Hello"; // s is an instance of BsonString

    BsonMaxKey, 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.Empty var id2 = ObjectId.Empty; // all zeroes var id3 = ObjectId.GenerateNewId(); // generates new unique Id var 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 shorter document.Add("age", 21); // creates BsonElement automatically

    BsonDocument

      BsonDocument是name/value键值对的集合。 BsonDocument构造函数
  • BsonDocument()
  • BsonDocument(string name, BsonValue value)
  • 上面是用的比较多

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Does Logitech ghub driver not support win7? -Why can Logitech ghub driver only be installed on the c drive? Does Logitech ghub driver not support win7? -Why can Logitech ghub driver only be installed on the c drive? Mar 18, 2024 pm 05:37 PM

    Does Logitech ghub driver not support win7? Not compatible. Since Windows 7 has stopped updating and is no longer Microsoft's main operating system, many new software no longer supports it, such as Logitech ghub. The main interface of the Logitech driver: 1. The main software interface is on the left. The three buttons are lighting, buttons, and sensitivity settings. 2. In the settings of the lighting interface, the general special effects are relatively conventional, and the audio visual effects are the highlight. They can change color according to the sound frequency, and can be set according to the high, middle and bass bands, with different colors and effects. 3. In button settings, users can edit them here according to their special requirements. 4. In the sensitivity settings, many users will have some of their own settings. They can add the DPI speed switching point by themselves, but

    Steps to restore Logitech driver to default configuration Steps to restore Logitech driver to default configuration Feb 28, 2024 am 11:04 AM

    Facing the Logitech driver that cannot be used normally, it can only be solved by restoring the factory settings. However, many friends do not know how to restore the factory settings. The following will provide you with detailed steps to restore the factory settings. I hope it can help you. Steps to restore the Logitech driver to the default configuration: 1. First download the Logitech dedicated driver software GHub, and open it after the download is complete. 2. Then open the mouse to set up, and click the settings (gear) in the upper right corner. 3. At the bottom, click "Restore default settings" , click directly and reopen the software to restore factory settings. How to connect the Logitech driver to the device 1. Open the back cover of the mouse and take out the wireless mouse socket. 2. Select the mouse socket next to the computer. 3. Set the corresponding parameter information for the mouse. 4. Bluetooth pair the device and long press Bluetooth.

    Which version is generally used for mongodb? Which version is generally used for mongodb? Apr 07, 2024 pm 05:48 PM

    It is recommended to use the latest version of MongoDB (currently 5.0) as it provides the latest features and improvements. When selecting a version, you need to consider functional requirements, compatibility, stability, and community support. For example, the latest version has features such as transactions and aggregation pipeline optimization. Make sure the version is compatible with the application. For production environments, choose the long-term support version. The latest version has more active community support.

    How to download Razer mouse driver How to download Razer mouse driver Mar 11, 2024 pm 03:40 PM

    Steps to download the Razer mouse driver: 1. Open the browser and enter the Razer official website; 2. On the official website page, find and click "Technical Support" or a similar option; 3. On the technical support page, select "Mouse" or the specific subcategory; 4. On the mouse driver download page, you can see various mouse models and their corresponding drivers; 5. Click the download link for the selected driver; 6. After the download is completed, check whether the downloaded file is complete , make sure nothing is damaged or missing.

    The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

    Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

    How to install win11 driver without digital signature_Tutorial on how to deal with win11 driver without digital signature How to install win11 driver without digital signature_Tutorial on how to deal with win11 driver without digital signature Mar 20, 2024 pm 04:46 PM

    Some users have encountered some problems when installing drivers for win11 computers. The computer prompts that the digital signature of this file cannot be verified, resulting in the inability to install the driver. How to solve this problem? Please see the following introduction for details. 1. Press the [Win + [Ctrl+Shift+Enter] Open the Windows Powershell window with administrator rights; 3. User Account Control window, do you want to allow this application to make changes to your device? Click [Yes]; 4. Administrator: Windows Powers

    Where is the database created by mongodb? Where is the database created by mongodb? Apr 07, 2024 pm 05:39 PM

    The data of the MongoDB database is stored in the specified data directory, which can be located in the local file system, network file system or cloud storage. The specific location is as follows: Local file system: The default path is Linux/macOS:/data/db, Windows: C:\data\db. Network file system: The path depends on the file system. Cloud Storage: The path is determined by the cloud storage provider.

    What are the advantages of mongodb database What are the advantages of mongodb database Apr 07, 2024 pm 05:21 PM

    The MongoDB database is known for its flexibility, scalability, and high performance. Its advantages include: a document data model that allows data to be stored in a flexible and unstructured way. Horizontal scalability to multiple servers via sharding. Query flexibility, supporting complex queries and aggregation operations. Data replication and fault tolerance ensure data redundancy and high availability. JSON support for easy integration with front-end applications. High performance for fast response even when processing large amounts of data. Open source, customizable and free to use.

    See all articles