同步查询一条数据
查询数据时,可以按照整行进行查询,也可以只查询某行的一个或几个字段(属性).
当我们查询整行时,直接使用FilterDefinition
设置好ObjectId
条件即可查询整行。
当我们查询一个或几个字段(属性)时,需要设置filter的同时,添加ProjectionDefinition
,指定好需要查询的字段(属性)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public T FindOne<T>(string collName,string objectId, string[]? fields = null) where T : class, new() { try { var collection = m_Database?.GetCollection<T>(collName); FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id",new ObjectId(objectId)); if(fields == null || fields.Length == 0) { return collection.Find(filter).FirstOrDefault<T>(); } var fieldList = new List<ProjectionDefinition<T>>(); foreach (string field in fields) { fieldList.Add(Builders<T>.Projection.Include(field)); } var projection = Builders<T>.Projection.Combine(fieldList); fieldList?.Clear(); return collection.Find(filter).Project<T>(projection).FirstOrDefault<T>(); } catch (Exception ex) { Debug.LogError("根据id查询数据出错:" + ex); throw; } }
|
异步查询一条数据
异步查询数据,将同步查询的FirstOrDefault
改为FirstOrDefaultAsync
即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public async Task<T> FindOneAsync<T>(string collName, string objectId, string[]? fields = null) where T : class, new() { try { var collection = m_Database?.GetCollection<T>(collName); FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id", new ObjectId(objectId)); if (fields == null || fields.Length == 0) { return await collection.Find(filter).FirstOrDefaultAsync<T>(); } var fieldList = new List<ProjectionDefinition<T>>(); foreach (string field in fields) { fieldList.Add(Builders<T>.Projection.Include(field)); } var projection = Builders<T>.Projection.Combine(fieldList); fieldList?.Clear(); return await collection.Find(filter).Project<T>(projection).FirstOrDefaultAsync<T>(); } catch (Exception ex) { Debug.LogError("根据id查询数据出错:" + ex); throw; } }
|
同步批量查询数据
批量查询数据,我们需要将FilterDefinition
作为参数,这个参数指定了批量查询的条件。我们还增加了SortDefinition
参数,指定了排序条件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public List<T> FindMany<T>(string collName, FilterDefinition<T> filter, string[]? fields = null, SortDefinition<T>? sort = null) where T : class, new() { try { var collection = m_Database?.GetCollection<T>(collName); if (fields == null || fields.Length == 0) { if(sort == null) return collection.Find(filter).ToList(); return collection.Find(filter).Sort(sort).ToList(); } var fieldList = new List<ProjectionDefinition<T>>(); foreach (string field in fields) { fieldList.Add(Builders<T>.Projection.Include(field)); } var projection = Builders<T>.Projection.Combine(fieldList); fieldList?.Clear();
if(sort == null) return collection.Find(filter).Project<T>(projection).ToList(); return collection.Find(filter).Sort(sort).Project<T>(projection).ToList(); } catch (Exception ex) { Debug.LogError("批量查询数据出错:" + ex); throw; } }
|
异步批量查询数据
将ToList
改为ToListAsync
即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public async Task<List<T>> FindManyAsync<T>(string collName, FilterDefinition<T> filter, string[]? fields = null, SortDefinition<T>? sort = null) where T : class, new() { try { var collection = m_Database?.GetCollection<T>(collName); if (fields == null || fields.Length == 0) { if (sort == null) return await collection.Find(filter).ToListAsync(); return await collection.Find(filter).Sort(sort).ToListAsync(); } var fieldList = new List<ProjectionDefinition<T>>(); foreach (string field in fields) { fieldList.Add(Builders<T>.Projection.Include(field)); } var projection = Builders<T>.Projection.Combine(fieldList); fieldList?.Clear();
if (sort == null) return await collection.Find(filter).Project<T>(projection).ToListAsync(); return await collection.Find(filter).Sort(sort).Project<T>(projection).ToListAsync(); } catch (Exception ex) { Debug.LogError("异步批量查询数据出错:" + ex); throw; } }
|