6
6
using System . Threading ;
7
7
using System . Threading . Tasks ;
8
8
using MongoDB . Bson . Serialization . Attributes ;
9
- using MongoDB . Driver ;
9
+ using MongoDB . Driver ;
10
+ using MongoDB . Driver . Linq ;
10
11
using PoweredSoft . DynamicLinq . Helpers ;
11
12
using PoweredSoft . ObjectStorage . Core ;
12
13
@@ -32,6 +33,11 @@ public MongoObjectStorageCollection(IMongoCollection<TEntity> collection)
32
33
return entity ;
33
34
}
34
35
36
+ public Task < bool > AnyAsync ( Expression < Func < TEntity , bool > > predicate , CancellationToken cancellationToken = default ( CancellationToken ) )
37
+ {
38
+ return MongoQueryable . AnyAsync ( Collection . AsQueryable ( ) , predicate , cancellationToken ) ;
39
+ }
40
+
35
41
public IQueryable < TEntity > AsQueryable ( )
36
42
{
37
43
return Collection . AsQueryable ( ) ;
@@ -95,6 +101,131 @@ public List<PropertyInfo> GetObjectKeys()
95
101
{
96
102
GetBsonIdProperty ( )
97
103
} ;
98
- }
104
+ }
105
+
106
+ public Task < TEntity > FirstOrDefaultAsync ( Expression < Func < TEntity , bool > > predicate , CancellationToken cancellationToken = default )
107
+ {
108
+ return this . Collection . AsQueryable ( ) . FirstOrDefaultAsync ( predicate , cancellationToken ) ;
109
+ }
110
+
111
+ public Task < TEntity > FirstAsync ( Expression < Func < TEntity , bool > > predicate , CancellationToken cancellationToken = default )
112
+ {
113
+ return this . Collection . AsQueryable ( ) . FirstAsync ( predicate , cancellationToken ) ;
114
+ }
115
+
116
+ public async Task UpdateManyAsync < TField > ( Expression < Func < TEntity , bool > > predicate , Expression < Func < TEntity , TField > > fieldExpression , TField value , CancellationToken cancellationToken = default )
117
+ {
118
+ var updateDefinition = Builders < TEntity > . Update . Set < TField > ( fieldExpression , value ) ;
119
+ await Collection . UpdateManyAsync ( predicate , updateDefinition , new UpdateOptions ( )
120
+ {
121
+ IsUpsert = false
122
+ } , cancellationToken ) ;
123
+ }
124
+
125
+ public async Task UpdateManyAsync < TField , TField2 > ( Expression < Func < TEntity , bool > > predicate ,
126
+ Expression < Func < TEntity , TField > > fieldExpression , TField value ,
127
+ Expression < Func < TEntity , TField2 > > fieldExpression2 , TField2 value2 ,
128
+
129
+ CancellationToken cancellationToken = default )
130
+ {
131
+ var updateDefinition = Builders < TEntity > . Update
132
+ . Set ( fieldExpression , value )
133
+ . Set ( fieldExpression2 , value2 )
134
+ ;
135
+
136
+ await Collection . UpdateManyAsync ( predicate , updateDefinition , new UpdateOptions ( )
137
+ {
138
+ IsUpsert = false
139
+ } , cancellationToken ) ;
140
+ }
141
+
142
+ public async Task UpdateManyAsync < TField , TField2 , TField3 > ( Expression < Func < TEntity , bool > > predicate ,
143
+ Expression < Func < TEntity , TField > > fieldExpression , TField value ,
144
+ Expression < Func < TEntity , TField2 > > fieldExpression2 , TField2 value2 ,
145
+ Expression < Func < TEntity , TField3 > > fieldExpression3 , TField3 value3 ,
146
+ CancellationToken cancellationToken = default )
147
+ {
148
+ var updateDefinition = Builders < TEntity > . Update
149
+ . Set ( fieldExpression , value )
150
+ . Set ( fieldExpression2 , value2 )
151
+ . Set ( fieldExpression3 , value3 )
152
+ ;
153
+
154
+ await Collection . UpdateManyAsync ( predicate , updateDefinition , new UpdateOptions ( )
155
+ {
156
+ IsUpsert = false
157
+ } , cancellationToken ) ;
158
+ }
159
+
160
+ public async Task UpdateOneAsync < TField > ( Expression < Func < TEntity , bool > > predicate , Expression < Func < TEntity , TField > > fieldExpression , TField value , CancellationToken cancellationToken = default )
161
+ {
162
+ var updateDefinition = Builders < TEntity > . Update . Set < TField > ( fieldExpression , value ) ;
163
+ await Collection . UpdateOneAsync ( predicate , updateDefinition , new UpdateOptions ( )
164
+ {
165
+ IsUpsert = false
166
+ } , cancellationToken ) ;
167
+ }
168
+
169
+ public async Task UpdateOneAsync < TField , TField2 > ( Expression < Func < TEntity , bool > > predicate ,
170
+ Expression < Func < TEntity , TField > > fieldExpression , TField value ,
171
+ Expression < Func < TEntity , TField2 > > fieldExpression2 , TField2 value2 ,
172
+ CancellationToken cancellationToken = default )
173
+ {
174
+ var updateDefinition = Builders < TEntity > . Update
175
+ . Set ( fieldExpression , value )
176
+ . Set ( fieldExpression2 , value2 ) ;
177
+
178
+ await Collection . UpdateOneAsync ( predicate , updateDefinition , new UpdateOptions ( )
179
+ {
180
+ IsUpsert = false
181
+ } , cancellationToken ) ;
182
+ }
183
+
184
+ public async Task UpdateOneAsync < TField , TField2 , TField3 > ( Expression < Func < TEntity , bool > > predicate ,
185
+ Expression < Func < TEntity , TField > > fieldExpression , TField value ,
186
+ Expression < Func < TEntity , TField2 > > fieldExpression2 , TField2 value2 ,
187
+ Expression < Func < TEntity , TField3 > > fieldExpression3 , TField3 value3 ,
188
+ CancellationToken cancellationToken = default )
189
+ {
190
+ var updateDefinition = Builders < TEntity > . Update
191
+ . Set ( fieldExpression , value )
192
+ . Set ( fieldExpression2 , value2 )
193
+ . Set ( fieldExpression3 , value3 )
194
+ ;
195
+
196
+ await Collection . UpdateOneAsync ( predicate , updateDefinition , new UpdateOptions ( )
197
+ {
198
+ IsUpsert = false
199
+ } , cancellationToken ) ;
200
+ }
201
+
202
+ public async Task UpdateOneAsync ( Expression < Func < TEntity , bool > > predicate , UpdateDefinition < TEntity > updateDefinition , CancellationToken cancellationToken = default )
203
+ {
204
+ await Collection . UpdateOneAsync ( predicate , updateDefinition , new UpdateOptions ( )
205
+ {
206
+ IsUpsert = false
207
+ } , cancellationToken ) ;
208
+ }
209
+
210
+ public async Task UpdateManyAsync ( Expression < Func < TEntity , bool > > predicate , UpdateDefinition < TEntity > updateDefinition , CancellationToken cancellationToken = default )
211
+ {
212
+ await Collection . UpdateManyAsync ( predicate , updateDefinition , new UpdateOptions ( )
213
+ {
214
+ IsUpsert = false
215
+ } , cancellationToken ) ;
216
+ }
217
+
218
+ public async Task < int > CountAsync ( Expression < Func < TEntity , bool > > predicate , CancellationToken cancellationToken = default )
219
+ {
220
+ var longResult = await Collection . CountDocumentsAsync ( predicate , null , cancellationToken ) ;
221
+ if ( longResult > int . MaxValue )
222
+ throw new Exception ( "Exceeds integer maximum value" ) ;
223
+ return ( int ) longResult ;
224
+ }
225
+
226
+ public Task < long > LongCountAsync ( Expression < Func < TEntity , bool > > predicate , CancellationToken cancellationToken = default )
227
+ {
228
+ return Collection . CountDocumentsAsync ( predicate , null , cancellationToken ) ;
229
+ }
99
230
}
100
231
}
0 commit comments