MongoDB C#驱动
+Driver+Tutorial 笔记 首先下载驱动。驱动有两个文件 MongoDB.Bson.dll MongoDB.Driver.dll 可以直接下载这两个驱动,或者按照下载源码进行编译生成。下载的源码可以看些test例子。 在新建的c#工程中添加这两个dll文件,并且使用如下命名空间 至少要引用如
+Driver+Tutorial
笔记
首先下载驱动。驱动有两个文件
可以直接下载这两个驱动,或者按照下载源码进行编译生成。下载的源码可以看些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 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.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 automaticallyBsonDocument
BsonDocument是name/value键值对的集合。 BsonDocument构造函数上面是用的比较多

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

MongoDB and relational database: In-depth comparison This article will explore in-depth the differences between NoSQL database MongoDB and traditional relational databases (such as MySQL and SQLServer). Relational databases use table structures of rows and columns to organize data, while MongoDB uses flexible document-oriented models to better suit the needs of modern applications. Mainly differentiates data structures: Relational databases use predefined schema tables to store data, and relationships between tables are established through primary keys and foreign keys; MongoDB uses JSON-like BSON documents to store them in a collection, and each document structure can be independently changed to achieve pattern-free design. Architectural design: Relational databases need to pre-defined fixed schema; MongoDB supports

To set up a MongoDB user, follow these steps: 1. Connect to the server and create an administrator user. 2. Create a database to grant users access. 3. Use the createUser command to create a user and specify their role and database access rights. 4. Use the getUsers command to check the created user. 5. Optionally set other permissions or grant users permissions to a specific collection.

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

The main tools for connecting to MongoDB are: 1. MongoDB Shell, suitable for quickly viewing data and performing simple operations; 2. Programming language drivers (such as PyMongo, MongoDB Java Driver, MongoDB Node.js Driver), suitable for application development, but you need to master the usage methods; 3. GUI tools (such as Robo 3T, Compass) provide a graphical interface for beginners and quick data viewing. When selecting tools, you need to consider application scenarios and technology stacks, and pay attention to connection string configuration, permission management and performance optimization, such as using connection pools and indexes.
