.NET MongoDB Driver 2.2 API注释
主要内容
1 MongoClient
1.1构造函数
1.2 方法
2 IMongoDatabase
3 IMongoCollection
4 IMongoCollectionExtensions
5 DeleteResult
6 UpdateResult
7 IFindFluent<TDocument, TDocument> 继承了
8 IFindFluentExtensions
9 IAsyncCursorSourceExtensions
10 Builders<DocumentInfo> 构造器
11 FilterDefinitionBuilder<TDocument>
12 FilterDefinition<TDocument>:基本过滤器
13 ProjectionDefinitionBuilder<TDocument>
14 SortDefinitionBuilder<TDocument>
15 UpdateDefinitionBuilder<TDocument>
16 BsonDocument:表示一个BSON文档
17 IBsonSerializerExtensions:扩展自IBsonSerializer
18 BsonDeserializationContext
19 AggregateOptions
1 MongoClient
1.1构造函数
1)public MongoClient(MongoClientSettings settings);
MongoClientSettings:和MongoUrl功能基本一致,但MongoClientSettings的属性多半是可修改类型。
2)public MongoClient(MongoUrl url);
MongoUrl :通过构造函数public MongoUrl(string url)设置连接utl。请注意,MongoUrl 的属性均为只读类型。
3)public MongoClient(string connectionString);
connectionString为连接字符串,标准连接字符串样式:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
参数说明:
mongodb://
必选。指明此链接字符串具有标准格式
username:password@
可选。如果指定,客户端将尝试使用这些凭证登陆到具体的数据库
host1
必选。指定了服务器连接地址。它确定了一个主机名,IP地址,或UNIX域套接字。
:port1
可选。默认值为27017,如果未指定则为默认值。
hostX
可选。你可以指定尽可能多的主机,您将指定多个主机,例如,连接到副本集。
/database
可选。用于验证的数据库名称,如果连接字符串包含username:password@格式的身份验证凭据。如果没有指定/database并且包含了身份验证凭据,驱动将会验证admin 数据库
?options
可选。格式为:name=value,使用&或;分隔每一对值。
例如:mongodb://192.168.22.246,192.168.22.245:2500/?replicaSet=test&connectTimeoutMS=300000
1)Replica Set Option
replicaSet:指定副本集的名称。
2)Connection Options
ssl:默认值是false,不启动TLS / SSL连接;值为ture时,启动TLS / SSL连接
connectTimeoutMS:连接超时值,默认永不超时。单位毫秒。
socketTimeoutMS:套接字超时值,默认永不超时。单位毫秒。
3)Connection Pool Options
maxPoolSize:连接池最大连接数,默认值为100。
minPoolSize:连接池最小连接数,默认值为0。
示例:
mongodb://test:cnki2016@192.168.22.26:27017/DBFIRST?maxPoolSize=100;minPoolSize=10
1.2 方法
1)public override sealed void DropDatabase(string name, CancellationToken cancellationToken = null)
删除数据库
参数:
name:数据库名称
cancellationToken:传播有关应取消操作的通知。
2)public override sealed IMongoDatabase GetDatabase(string name, MongoDatabaseSettings settings = null);
获得数据库
参数:
name:数据库名称
settings:数据库设置
2 IMongoDatabase
1)void CreateCollection(string name, CreateCollectionOptions options = null, CancellationToken cancellationToken = null)
创建集合
参数:
name:集合名称
Options:创建集合时的待选参数
cancellationToken:传播有关应取消操作的通知
2)void DropCollection(string name, CancellationToken cancellationToken = null);
删除集合
参数:
name:集合名称
cancellationToken:传播有关应取消操作的通知
3)IMongoCollection<TDocument> GetCollection<TDocument>(string name, MongoCollectionSettings settings = null)
获得集合
参数:
TDocument:文档类型
name:集合名称
settings:数据库设置
4)void RenameCollection(string oldName, string newName, RenameCollectionOptions options = null, CancellationToken cancellationToken = null)
重命名集合
参数:
oldName:集合旧的名称
newName:集合新名称
options:重命名时的设置参数
cancellationToken:传播有关应取消操作的通知
3 IMongoCollection
1)DeleteResult DeleteMany(FilterDefinition<TDocument> filter, CancellationToken cancellationToken = null)
删除多个文档,将过滤出的文档全部删除
参数:
TDocument:文档类型
filter:过滤器
cancellationToken:传播有关应取消操作的通知
2) DeleteResult DeleteOne(FilterDefinition<TDocument> filter, CancellationToken cancellationToken = null)
删除一个文档,将过滤出的文档中第一个删除。
参数:
TDocument:文档类型
filter:过滤器
cancellationToken:传播有关应取消操作的通知
3) void InsertMany(IEnumerable<TDocument> documents, InsertManyOptions options = null, CancellationToken cancellationToken = null)
插入多个文档
参数:
TDocument:文档类型
documents:待插入文档
options:插入操作设置参数
cancellationToken:传播有关应取消操作的通知
4) void InsertOne(TDocument document, InsertOneOptions options = null, CancellationToken cancellationToken = null)
插入一个文档
参数:
documents:待插入文档
options:插入操作设置参数
cancellationToken:传播有关应取消操作的通知
5) UpdateResult UpdateMany(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = null)
更新多个文档,将过滤出的多个文档全部更新
参数:
TDocument:文档类型
filter:过滤器
update:更新过滤器
options:插入操作设置参数
cancellationToken:传播有关应取消操作的通知
6) UpdateResult UpdateOne(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = null)
更新一个文档,将过滤出的多个文档中的第一个更新
参数:
TDocument:文档类型
filter:过滤器
update:更新过滤器
options:插入操作设置参数
cancellationToken:传播有关应取消操作的通知
7) long Count(FilterDefinition<TDocument> filter, CountOptions options = null, CancellationToken cancellationToken = null)
获得文档数量
参数:
TDocument:文档类型
filter:过滤器
options:插入操作设置参数
cancellationToken:传播有关应取消操作的通知
8) Task InsertManyAsync(IEnumerable<TDocument> documents, InsertManyOptions options = null, CancellationToken cancellationToken = null)
已异步的方式插入多个文档
参数:
TDocument:文档类型
documents:待插入文档
options:插入操作设置参数
cancellationToken:传播有关应取消操作的通知
9) Task InsertOneAsync(TDocument document, InsertOneOptions options = null, CancellationToken cancellationToken = null)
以异步方式插入一个文档
参数:
TDocument:文档类型
documents:待插入文档
options:插入操作设置参数
cancellationToken:传播有关应取消操作的通知
10)IBsonSerializer<TDocument> DocumentSerializer { get; }
获得文档序列化器
11)IAsyncCursor<TResult> Aggregate<TResult>(PipelineDefinition<TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = null)
聚集操作
参数:
TResult:返回结果类型
TDocument:输入文档类型
pipeline:管道
options :设置参数
cancellationToken :取消标记
4 IMongoCollectionExtensions
1)public static IFindFluent<TDocument, TDocument> Find<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOptions options = null)
找到文档
参数:
TDocument:文档类型
collection:集合
filter:查找条件
options:查找操作设置参数
2)public static IFindFluent<TDocument, TDocument> Find<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions options = null)
找到文档
参数:
TDocument:文档类型
collection:集合
filter:查找条件
options:查找操作设置参数
5 DeleteResult
1)public abstract long DeletedCount { get; }
获得删除的条数,如果IsAcknowledged的值为false,将抛出异常
2)public abstract bool IsAcknowledged { get; }
结果是否被承认
6 UpdateResult
1)public abstract bool IsAcknowledged { get; }
结果是否被承认
2)public abstract bool IsModifiedCountAvailable { get; }
是否可以获得修改的数量
3)public abstract long MatchedCount { get; }
匹配到的数量
4)public abstract long ModifiedCount { get; }
修改的数量
5)public abstract BsonValue UpsertedId { get; }
获得更新插入的id
7 IFindFluent<TDocument, TDocument> 继承了IAsyncCursorSource<TProjection>
1)IFindFluent<TDocument, TProjection> Limit(int? limit)
限制取出的文档数量
参数:
TDocument:文档类型
TProjection:投影类型,如果没有投影那么其类型和TDocument相同
limit:取出文档数量
2)IFindFluent<TDocument, TNewProjection> Project<TNewProjection>(ProjectionDefinition<TDocument, TNewProjection> projection)
对找到的文档进行投影操作
参数:
TDocument:文档类型
TNewProjection:投影类型,如果没有投影那么其类型和TDocument相同
projection:投影
3)IFindFluent<TDocument, TProjection> Skip(int? skip)
跳过一定数量的文档
参数:
TDocument:文档类型
TProjection:投影类型,如果没有投影那么其类型和TDocument相同
skip:跳过的条数
4)IFindFluent<TDocument, TProjection> Sort(SortDefinition<TDocument> sort)
对找到的文档排序
参数:
TDocument:文档类型
TProjection:投影类型,如果没有投影那么其类型和TDocument相同
sort:排序定义
8 IFindFluentExtensions
public static TProjection First<TDocument, TProjection>(this IFindFluent<TDocument, TProjection> find, CancellationToken cancellationToken = null)
获得找到的第一个元素。
参数:
TDocument:文档类型
TProjection:投影类型,如果没有投影那么其类型和TDocument相同
find:查找条件
cancellationToken:传播有关应取消操作的通知
9 IAsyncCursorSourceExtensions
public static List<TDocument> ToList<TDocument>(this IAsyncCursorSource<TDocument> source, CancellationToken cancellationToken = null)
将IAsyncCursorSource<TDocument> source转换为List<TDocument>
参数:
TDocument:文档类型
source:待转换集合
cancellationToken:传播有关应取消操作的通知
-----------------------------------------------------------------------------------------
转载与引用请注明出处。
时间仓促,水平有限,如有不当之处,欢迎指正。
.NET MongoDB Driver 2.2 API注释的更多相关文章
- c# MongoDB Driver 官方教程翻译
先贴官方文档地址:http://mongodb.github.io/mongo-csharp-driver/2.5/getting_started/quick_tour/ 安装部分很简单,nuget搜 ...
- 基于MongoDB.Driver的扩展
由于MongoDB.Driver中的Find方法也支持表达式写法,结合[通用查询设计思想]这篇文章中的查询思想,个人基于MongoDB扩展了一些常用的方法. 首先我们从常用的查询开始,由于MongoD ...
- MongoDB.Driver 2.4以上版本 在.NET中的基本操作
MongoDB.Driver是操作mongo数据库的驱动,最近2.0以下版本已经从GitHub和Nuget中移除了,也就是说.NET Framework4.0不再能从官方获取到MongoDB的驱动了, ...
- MongoDB Driver 简单的CURD
c#中我们可以使用MongoDB.Driver驱动进行对MongoDB数据库的增删改查. 首先需要在NuGet中安装驱动 安装完毕后会发现会有三个引用 其中 MongoDB.Driver和MongoD ...
- MongoDB系列:五、MongoDB Driver使用正确的姿势连接复制集
MongoDB复制集(Replica Set)通过存储多份数据副本来保证数据的高可靠,通过自动的主备切换机制来保证服务的高可用.但需要注意的时,连接副本集的姿势如果不对,服务高可用将不复存在. 使用复 ...
- C# mongoDB Driver 使用对象方式查询语法大全
#region 查询方法 /// <summary> /// 获取单个对象 /// </summary> /// <typeparam name="T" ...
- php MongoDB driver 查询实例
//是否只查mx $mx_on_switch = I("post.mx_on_switch"); //mx模糊查询 $mx_vague_check = I("post.m ...
- PHP7 - MongoDB Driver 使用心得
php7 只能使用Mongodb driver来驱动mongodb. 使用Mongodb Driver连接数据库 刚开始使用Mongodb Driver的时候我是拒绝的.查看官方文档只看到一排的类和不 ...
- MongoDB入门及 c# .netcore客户端MongoDB.Driver使用
MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...
随机推荐
- css em单位
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/41 为什么要有em 为了弹性布局.更准确的说是界面元素根据浏览器 ...
- Selinux安全机制
1.Selinux安全机制简介 Selinux是Google在Android 4.4上正式推出的一套以SELinux为基础于核心的系统安全机制.而SELinux则是由美国NSA(国安局)和一些公司(R ...
- C++ qsort
使用qsort 需要包含头文件#include<algorithm> 例子: class Wooden{ public: int weight; int length; bool flag ...
- php curl 请求302跳转页面
今天对接支付接口,需要获取支付页面,发现支付商那边给的链接会发送302 跳转,最后发现该方法,绝对给力: <?php $url = 'http://auto.jrj.com.cn/'; $ch ...
- c3p0使用记录
首先要导入c3p0包.c3p0下载解压后,lib目录下有三个包,使用mysql的话,只需要导入c3p0-0.9.5.2.jar,mchange-commons-java-0.2.11.jar. 要连接 ...
- gitlab markdown支持页面内跳转
markdown语法: [to_be_link](#id_name) 标题: ## 2.aaa <a name="id_name"></a> 参考: htt ...
- hadoop+hive+spark搭建(二)
上传hive软件包到任意节点 一.安装hive软件 解压缩hive软件包到/usr/local/hadoop/目录下 重命名hive文件夹 在/etc/profile文件中添加环境变量 export ...
- JavaScript Html页面加载完成三种写法
//一.Html页面加载完成的JS写法 //1. $(function () { alert("窗体Html页面加载完成方法一"); }); //2. $(document ...
- C# 读取系统日志
.NET框架类库提供了EventLog类和EventLogEntry类与系统日志进行交互二者属于System.Diagnostics命名空间 EventLog 类的属性主要有 Entris返回一个Ev ...
- Java学习笔记4(方法)
方法和c++中的函数类似,区别在于java的方法定义不限位置,而c++中的定义在主函数后面的函数调用前要声明: 求矩形面积方法示例: public class MethodDemo{ public s ...