MongoDataBase 类
使用 MongoDB 数据库的实现。
实现
继承成员
命名空间: Fantasy.DataBase
程序集: Fantasy.Net.dll
语法
public sealed class MongoDataBase : IDateBase
方法
| 编辑本文 查看源代码Count<T>(Expression<Func<T, bool>>, string)
统计指定集合中满足条件的文档数量。
声明
public FTask<long> Count<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 用于筛选文档的表达式。 |
string | collection | 集合名称,可选。如果未指定,将使用实体类型的名称。 |
返回
类型 | 描述 |
---|---|
FTask<long> | 满足条件的文档数量。 |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
Count<T>(string)
统计指定集合中满足条件的文档数量。
声明
public FTask<long> Count<T>(string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
string | collection | 集合名称,可选。如果未指定,将使用实体类型的名称。 |
返回
类型 | 描述 |
---|---|
FTask<long> | 满足条件的文档数量。 |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
CreateDB(Type)
创建数据库集合(如果不存在)。
声明
public FTask CreateDB(Type type)
参数
类型 | 名称 | 描述 |
---|---|---|
Type | type | 实体类型。 |
返回
类型 | 描述 |
---|---|
FTask |
CreateDB<T>()
创建数据库集合(如果不存在)。
声明
public FTask CreateDB<T>() where T : Entity
返回
类型 | 描述 |
---|---|
FTask |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
CreateIndex<T>(params object[])
创建数据库的索引(加锁)。
声明
public FTask CreateIndex<T>(params object[] keys) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
object[] | keys | 索引键定义。 |
返回
类型 | 描述 |
---|---|
FTask |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
CreateIndex<T>(string, params object[])
创建数据库索引(加锁)。
声明
public FTask CreateIndex<T>(string collection, params object[] keys) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
string | collection | |
object[] | keys |
返回
类型 | 描述 |
---|---|
FTask |
类型参数
名称 | 描述 |
---|---|
T |
Exist<T>(Expression<Func<T, bool>>, string)
判断指定集合中是否存在满足条件的文档。
声明
public FTask<bool> Exist<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 用于筛选文档的表达式。 |
string | collection | 集合名称,可选。如果未指定,将使用实体类型的名称。 |
返回
类型 | 描述 |
---|---|
FTask<bool> | 如果存在满足条件的文档则返回 true,否则返回 false。 |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
Exist<T>(string)
判断指定集合中是否存在文档。
声明
public FTask<bool> Exist<T>(string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
string | collection | 集合名称,可选。如果未指定,将使用实体类型的名称。 |
返回
类型 | 描述 |
---|---|
FTask<bool> | 如果存在文档则返回 true,否则返回 false。 |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
First<T>(Expression<Func<T, bool>>, string)
通过指定过滤条件查询并返回满足条件的第一个文档(加锁)。
声明
public FTask<T?> First<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 查询过滤条件。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<T> | 满足条件的第一个文档,如果未找到则为 null。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
First<T>(string, string[], string)
通过指定 JSON 格式查询并返回满足条件的第一个文档(加锁)。
声明
public FTask<T> First<T>(string json, string[] cols, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
string | json | JSON 查询条件。 |
string[] | cols | 要查询的列名称数组。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<T> | 满足条件的第一个文档。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
Initialize(Scene, string, string)
初始化 MongoDB 数据库连接并记录所有集合名。
声明
public IDateBase Initialize(Scene scene, string connectionString, string dbName)
参数
类型 | 名称 | 描述 |
---|---|---|
Scene | scene | 所在的Scene。 |
string | connectionString | 数据库连接字符串。 |
string | dbName | 数据库名称。 |
返回
类型 | 描述 |
---|---|
IDateBase | 初始化后的数据库实例。 |
InsertBatch<T>(IEnumerable<T>, string)
批量插入实体对象列表到数据库(加锁)。
声明
public FTask InsertBatch<T>(IEnumerable<T> list, string collection = null) where T : Entity, new()
参数
类型 | 名称 | 描述 |
---|---|---|
IEnumerable<T> | list | 要插入的实体对象列表。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
InsertBatch<T>(object, IEnumerable<T>, string)
批量插入实体对象列表到数据库(加锁)。
声明
public FTask InsertBatch<T>(object transactionSession, IEnumerable<T> list, string collection = null) where T : Entity, new()
参数
类型 | 名称 | 描述 |
---|---|---|
object | transactionSession | 事务会话对象。 |
IEnumerable<T> | list | 要插入的实体对象列表。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
Insert<T>(T, string)
插入单个实体对象到数据库(加锁)。
声明
public FTask Insert<T>(T entity, string collection = null) where T : Entity, new()
参数
类型 | 名称 | 描述 |
---|---|---|
T | entity | 要插入的实体对象。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
Query(long, List<string>, List<Entity>)
根据指定 ID 加锁查询多个集合中的文档。
声明
public FTask Query(long id, List<string> collectionNames, List<Entity> result)
参数
类型 | 名称 | 描述 |
---|---|---|
long | id | 文档 ID。 |
List<string> | collectionNames | 要查询的集合名称列表。 |
List<Entity> | result | 查询结果存储列表。 |
返回
类型 | 描述 |
---|---|
FTask |
QueryByPageOrderBy<T>(Expression<Func<T, bool>>, int, int, Expression<Func<T, object>>, bool, string)
通过分页查询并返回满足条件的文档列表,并按指定表达式进行排序(加锁)。
声明
public FTask<List<T>> QueryByPageOrderBy<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, Expression<Func<T, object>> orderByExpression, bool isAsc = true, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 查询过滤条件。 |
int | pageIndex | 页码。 |
int | pageSize | 每页大小。 |
Expression<Func<T, object>> | orderByExpression | 排序表达式。 |
bool | isAsc | 是否升序排序。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<List<T>> | 满足条件的文档列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
QueryByPage<T>(Expression<Func<T, bool>>, int, int, string)
通过分页查询并返回满足条件的文档列表(不加锁)。
声明
public FTask<List<T>> QueryByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 查询过滤条件。 |
int | pageIndex | 页码。 |
int | pageSize | 每页大小。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<List<T>> | 满足条件的文档列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
QueryByPage<T>(Expression<Func<T, bool>>, int, int, string[], string)
通过分页查询并返回满足条件的文档列表(加锁)。
声明
public FTask<List<T>> QueryByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string[] cols, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 查询过滤条件。 |
int | pageIndex | 页码。 |
int | pageSize | 每页大小。 |
string[] | cols | 要查询的列名称数组。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<List<T>> | 满足条件的文档列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
QueryCountAndDatesByPage<T>(Expression<Func<T, bool>>, int, int, string)
通过分页查询并返回满足条件的文档数量和日期列表(不加锁)。
声明
public FTask<(int count, List<T> dates)> QueryCountAndDatesByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 查询过滤条件。 |
int | pageIndex | 页码。 |
int | pageSize | 每页大小。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<(int count, List<T> dates)> | 满足条件的文档数量和日期列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
QueryCountAndDatesByPage<T>(Expression<Func<T, bool>>, int, int, string[], string)
通过分页查询并返回满足条件的文档数量和日期列表(加锁)。
声明
public FTask<(int count, List<T> dates)> QueryCountAndDatesByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string[] cols, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 查询过滤条件。 |
int | pageIndex | 页码。 |
int | pageSize | 每页大小。 |
string[] | cols | 要查询的列名称数组。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<(int count, List<T> dates)> | 满足条件的文档数量和日期列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
QueryJson<T>(long, string, string)
根据指定的 JSON 查询条件和任务 ID 查询并返回满足条件的文档列表(加锁)。
声明
public FTask<List<T>> QueryJson<T>(long taskId, string json, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
long | taskId | 任务 ID。 |
string | json | JSON 查询条件。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<List<T>> | 满足条件的文档列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
QueryJson<T>(string, string)
根据指定的 JSON 查询条件查询并返回满足条件的文档列表(加锁)。
声明
public FTask<List<T>> QueryJson<T>(string json, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
string | json | JSON 查询条件。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<List<T>> | 满足条件的文档列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
QueryJson<T>(string, string[], string)
根据指定的 JSON 查询条件查询并返回满足条件的文档列表,并选择指定的列(加锁)。
声明
public FTask<List<T>> QueryJson<T>(string json, string[] cols, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
string | json | JSON 查询条件。 |
string[] | cols | 要查询的列名称数组。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<List<T>> | 满足条件的文档列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
QueryNotLock<T>(long, string)
在不加数据库锁定的情况下,查询指定 ID 的文档。
声明
public FTask<T> QueryNotLock<T>(long id, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
long | id | 要查询的文档 ID。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<T> | 查询到的文档。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
QueryOrderBy<T>(Expression<Func<T, bool>>, Expression<Func<T, object>>, bool, string)
通过指定过滤条件查询并返回满足条件的文档列表,并按指定表达式进行排序(加锁)。
声明
public FTask<List<T>> QueryOrderBy<T>(Expression<Func<T, bool>> filter, Expression<Func<T, object>> orderByExpression, bool isAsc = true, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 查询过滤条件。 |
Expression<Func<T, object>> | orderByExpression | 排序表达式。 |
bool | isAsc | 是否升序排序。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<List<T>> | 满足条件的文档列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
Query<T>(long, string)
查询指定 ID 的文档,并加数据库锁定以确保数据一致性。
声明
public FTask<T> Query<T>(long id, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
long | id | 要查询的文档 ID。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<T> | 查询到的文档。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
Query<T>(Expression<Func<T, bool>>, string)
通过指定过滤条件查询并返回满足条件的文档列表(加锁)。
声明
public FTask<List<T>> Query<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 查询过滤条件。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<List<T>> | 满足条件的文档列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
Query<T>(Expression<Func<T, bool>>, string[], string)
根据指定过滤条件查询并返回满足条件的文档列表,选择指定的列(加锁)。
声明
public FTask<List<T>> Query<T>(Expression<Func<T, bool>> filter, string[] cols, string collection = null) where T : class
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 查询过滤条件。 |
string[] | cols | 要查询的列名称数组。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<List<T>> | 满足条件的文档列表。 |
类型参数
名称 | 描述 |
---|---|
T | 文档实体类型。 |
Remove<T>(long, Expression<Func<T, bool>>, string)
根据ID和筛选条件删除多个实体对象(加锁)。
声明
public FTask<long> Remove<T>(long coroutineLockQueueKey, Expression<Func<T, bool>> filter, string collection = null) where T : Entity, new()
参数
类型 | 名称 | 描述 |
---|---|---|
long | coroutineLockQueueKey | 异步锁Id。 |
Expression<Func<T, bool>> | filter | 筛选条件。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<long> | 删除的实体数量。 |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
Remove<T>(long, object, Expression<Func<T, bool>>, string)
根据ID和筛选条件删除多个实体对象(加锁)。
声明
public FTask<long> Remove<T>(long coroutineLockQueueKey, object transactionSession, Expression<Func<T, bool>> filter, string collection = null) where T : Entity, new()
参数
类型 | 名称 | 描述 |
---|---|---|
long | coroutineLockQueueKey | 异步锁Id。 |
object | transactionSession | 事务会话对象。 |
Expression<Func<T, bool>> | filter | 筛选条件。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<long> | 删除的实体数量。 |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
Remove<T>(long, string)
根据ID删除单个实体对象(加锁)。
声明
public FTask<long> Remove<T>(long id, string collection = null) where T : Entity, new()
参数
类型 | 名称 | 描述 |
---|---|---|
long | id | 要删除的实体的ID。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<long> | 删除的实体数量。 |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
Remove<T>(object, long, string)
根据ID删除单个实体对象(加锁)。
声明
public FTask<long> Remove<T>(object transactionSession, long id, string collection = null) where T : Entity, new()
参数
类型 | 名称 | 描述 |
---|---|---|
object | transactionSession | 事务会话对象。 |
long | id | 要删除的实体的ID。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask<long> | 删除的实体数量。 |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
Save(long, List<Entity>)
保存多个实体对象到数据库(加锁)。
声明
public FTask Save(long id, List<Entity> entities)
参数
类型 | 名称 | 描述 |
---|---|---|
long | id | 文档 ID。 |
List<Entity> | entities | 要保存的实体对象列表。 |
返回
类型 | 描述 |
---|---|
FTask |
Save<T>(object, T, string)
保存实体对象到数据库(加锁)。
声明
public FTask Save<T>(object transactionSession, T entity, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
object | transactionSession | 事务会话对象。 |
T | entity | 要保存的实体对象。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
Save<T>(T, string)
保存实体对象到数据库(加锁)。
声明
public FTask Save<T>(T entity, string collection = null) where T : Entity, new()
参数
类型 | 名称 | 描述 |
---|---|---|
T | entity | 要保存的实体对象。 |
string | collection | 集合名称。 |
返回
类型 | 描述 |
---|---|
FTask |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |
Sum<T>(Expression<Func<T, bool>>, Expression<Func<T, object>>, string)
对满足条件的文档中的某个数值字段进行求和操作。
声明
public FTask<long> Sum<T>(Expression<Func<T, bool>> filter, Expression<Func<T, object>> sumExpression, string collection = null) where T : Entity
参数
类型 | 名称 | 描述 |
---|---|---|
Expression<Func<T, bool>> | filter | 用于筛选文档的表达式。 |
Expression<Func<T, object>> | sumExpression | 要对其进行求和的字段表达式。 |
string | collection | 集合名称,可选。如果未指定,将使用实体类型的名称。 |
返回
类型 | 描述 |
---|---|
FTask<long> | 满足条件的文档中指定字段的求和结果。 |
类型参数
名称 | 描述 |
---|---|
T | 实体类型。 |