Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 633 lines (564 sloc) 25.579 kB
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
4 using System.Linq;
5 using MongoDB.Configuration;
a054c05 @lanwin Fix namespaces.
lanwin authored
6 using MongoDB.Connections;
7 using MongoDB.Protocol;
8 using MongoDB.Results;
9 using MongoDB.Util;
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
10
a054c05 @lanwin Fix namespaces.
lanwin authored
11 namespace MongoDB
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
12 {
13 /// <summary>
14 ///
15 /// </summary>
16 public class MongoCollection<T> : IMongoCollection<T> where T : class
17 {
391fe44 @lanwin Merge with craig.
lanwin authored
18 private readonly MongoConfiguration _configuration;
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
19 private readonly Connection _connection;
20 private MongoDatabase _database;
ab343f8 @lanwin Rename MetaData propertys to Metadata.
lanwin authored
21 private CollectionMetadata _metadata;
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
22
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
23 /// <summary>
24 /// Initializes a new instance of the <see cref="MongoCollection&lt;T&gt;"/> class.
25 /// </summary>
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
26 /// <param name="configuration">The configuration.</param>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
27 /// <param name="connection">The connection.</param>
28 /// <param name="databaseName">Name of the database.</param>
c2f6d87 @lanwin Start making not users types internal.
lanwin authored
29 /// <param name="collectionName">The name.</param>
30 internal MongoCollection(MongoConfiguration configuration, Connection connection, string databaseName, string collectionName)
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
31 {
c2f6d87 @lanwin Start making not users types internal.
lanwin authored
32 //Todo: add public constructors for users to call
33 Name = collectionName;
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
34 DatabaseName = databaseName;
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
35 _configuration = configuration;
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
36 _connection = connection;
37 }
38
39 /// <summary>
40 /// Gets the database.
41 /// </summary>
42 /// <value>The database.</value>
43 public IMongoDatabase Database {
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
44 get { return _database ?? (_database = new MongoDatabase(_configuration, _connection, DatabaseName)); }
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
45 }
46
47 /// <summary>
48 /// Gets or sets the name.
49 /// </summary>
50 /// <value>The name.</value>
51 public string Name { get; private set; }
52
53 /// <summary>
54 /// Gets or sets the name of the database.
55 /// </summary>
56 /// <value>The name of the database.</value>
57 public string DatabaseName { get; private set; }
58
59 /// <summary>
60 /// Gets the full name including database name.
61 /// </summary>
62 /// <value>The full name.</value>
63 public string FullName {
64 get { return DatabaseName + "." + Name; }
65 }
66
67 /// <summary>
68 /// Gets the meta data.
69 /// </summary>
70 /// <value>The meta data.</value>
ab343f8 @lanwin Rename MetaData propertys to Metadata.
lanwin authored
71 public CollectionMetadata Metadata {
72 get { return _metadata ?? (_metadata = new CollectionMetadata(_configuration, DatabaseName, Name, _connection)); }
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
73 }
74
75 /// <summary>
79eebe4 @lanwin Add FindOne(string javascriptWhere)
lanwin authored
76 /// Finds and returns the first document in a selector query.
77 /// </summary>
78 /// <param name="javascriptWhere">The where.</param>
79 /// <returns>
80 /// A <see cref="Document"/> from the collection.
81 /// </returns>
82 public T FindOne(string javascriptWhere)
83 {
84 var spec = new Document { { "$where", new Code(javascriptWhere) } };
85 using(var cursor = Find(spec, -1, 0, null))
86 return cursor.Documents.FirstOrDefault();
87 }
88
89 /// <summary>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
90 /// Finds and returns the first document in a query.
91 /// </summary>
92 /// <param name="spec">A <see cref="Document"/> representing the query.</param>
93 /// <returns>
94 /// A <see cref="Document"/> from the collection.
95 /// </returns>
96 public T FindOne(object spec){
79eebe4 @lanwin Add FindOne(string javascriptWhere)
lanwin authored
97 using(var cursor = Find(spec, -1, 0, null))
98 return cursor.Documents.FirstOrDefault();
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
99 }
100
101 /// <summary>
102 /// Finds all.
103 /// </summary>
104 /// <returns></returns>
105 public ICursor<T> FindAll(){
106 var spec = new Document();
107 return Find(spec, 0, 0, null);
108 }
109
110 /// <summary>
111 /// Finds the specified where.
112 /// </summary>
79eebe4 @lanwin Add FindOne(string javascriptWhere)
lanwin authored
113 /// <param name="javascriptWhere">The where.</param>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
114 /// <returns></returns>
79eebe4 @lanwin Add FindOne(string javascriptWhere)
lanwin authored
115 public ICursor<T> Find(string javascriptWhere){
116 var spec = new Document { { "$where", new Code(javascriptWhere) } };
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
117 return Find(spec, 0, 0, null);
118 }
119
120 /// <summary>
121 /// Finds the specified spec.
122 /// </summary>
123 /// <param name="spec">The spec.</param>
124 /// <returns></returns>
125 public ICursor<T> Find(object spec){
126 return Find(spec, 0, 0, null);
127 }
128
129 /// <summary>
130 /// Finds the specified spec.
131 /// </summary>
132 /// <param name="spec">The spec.</param>
133 /// <param name="fields"></param>
134 /// <returns>A <see cref="ICursor"/></returns>
135 public ICursor<T> Find(object spec, object fields){
136 return Find(spec, 0, 0, fields);
137 }
138
139 /// <summary>
140 /// Finds the specified spec.
141 /// </summary>
142 /// <param name="spec">The spec.</param>
143 /// <param name="limit">The limit.</param>
144 /// <param name="skip">The skip.</param>
145 /// <returns></returns>
146 public ICursor<T> Find(object spec, int limit, int skip){
147 return Find(spec, limit, skip, null);
148 }
149
150 /// <summary>
151 /// Finds the specified spec.
152 /// </summary>
153 /// <param name="spec">The spec.</param>
154 /// <param name="limit">The limit.</param>
155 /// <param name="skip">The skip.</param>
156 /// <param name="fields">The fields.</param>
157 /// <returns></returns>
158 public ICursor<T> Find(object spec, int limit, int skip, object fields){
159 if (spec == null)
160 spec = new Document();
af265ba @craiggwilson fixed projection problems with inheritance
craiggwilson authored
161 return new Cursor<T>(_configuration.SerializationFactory, _configuration.MappingStore, _connection, DatabaseName, Name, spec, limit, skip, fields);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
162 }
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
163
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
164 /// <summary>
165 /// Executes a query and atomically applies a modifier operation to the first document returning the original document
166 /// by default.
167 /// </summary>
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
168 /// <param name="document">The document.</param>
169 /// <param name="spec"><see cref="Document"/> to find the document.</param>
170 /// <returns>A <see cref="Document"/></returns>
171 public T FindAndModify(object document, object spec){
172 return FindAndModify(document, spec, false);
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
173 }
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
174
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
175 /// <summary>
176 /// Executes a query and atomically applies a modifier operation to the first document returning the original document
177 /// by default.
178 /// </summary>
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
179 /// <param name="document">The document.</param>
180 /// <param name="spec"><see cref="Document"/> to find the document.</param>
181 /// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the</param>
182 /// <returns>A <see cref="Document"/></returns>
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
183 /// <see cref="IndexOrder"/>
6397a0f @lanwin More find and modify improvments.
lanwin authored
184 public T FindAndModify(object document, object spec, object sort)
185 {
186 return FindAndModify(document, spec, sort, null, false, false, false);
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
187 }
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
188
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
189 /// <summary>
190 /// Executes a query and atomically applies a modifier operation to the first document returning the original document
191 /// by default.
192 /// </summary>
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
193 /// <param name="document">The document.</param>
194 /// <param name="spec"><see cref="Document"/> to find the document.</param>
195 /// <param name="returnNew">if set to <c>true</c> [return new].</param>
196 /// <returns>A <see cref="Document"/></returns>
6397a0f @lanwin More find and modify improvments.
lanwin authored
197 public T FindAndModify(object document, object spec, bool returnNew)
198 {
199 return FindAndModify(document, spec, null, null, false, returnNew, false);
e09bd0d @lanwin FindAndModify add fields and upsert.
lanwin authored
200 }
201 /// <summary>
202 /// Executes a query and atomically applies a modifier operation to the first document returning the original document
203 /// by default.
204 /// </summary>
205 /// <param name="document">The document.</param>
206 /// <param name="spec"><see cref="Document"/> to find the document.</param>
207 /// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the
208 /// <see cref="IndexOrder"/></param>
209 /// <param name="returnNew">if set to <c>true</c> [return new].</param>
210 /// <returns>A <see cref="Document"/></returns>
211 public T FindAndModify(object document, object spec, object sort, bool returnNew)
212 {
6397a0f @lanwin More find and modify improvments.
lanwin authored
213 return FindAndModify(document, spec, sort, null, false, returnNew, false);
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
214 }
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
215
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
216 /// <summary>
217 /// Executes a query and atomically applies a modifier operation to the first document returning the original document
218 /// by default.
219 /// </summary>
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
220 /// <param name="document">The document.</param>
221 /// <param name="spec"><see cref="Document"/> to find the document.</param>
222 /// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the
223 /// <see cref="IndexOrder"/></param>
e09bd0d @lanwin FindAndModify add fields and upsert.
lanwin authored
224 /// <param name="fields">The fields.</param>
6397a0f @lanwin More find and modify improvments.
lanwin authored
225 /// <param name="remove">if set to <c>true</c> [remove].</param>
226 /// <param name="returnNew">if set to <c>true</c> [return new].</param>
e09bd0d @lanwin FindAndModify add fields and upsert.
lanwin authored
227 /// <param name="upsert">if set to <c>true</c> [upsert].</param>
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
228 /// <returns>A <see cref="Document"/></returns>
6397a0f @lanwin More find and modify improvments.
lanwin authored
229 public T FindAndModify(object document, object spec, object sort, object fields, bool remove, bool returnNew, bool upsert)
e09bd0d @lanwin FindAndModify add fields and upsert.
lanwin authored
230 {
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
231 try
232 {
9ce6e59 @lanwin Move Hash function to its own class.
lanwin authored
233 var command = new Document
e543727 @craiggwilson cleaning up a little
craiggwilson authored
234 {
235 {"findandmodify", Name},
236 {"query", spec},
237 {"update", EnsureUpdateDocument(document)},
e09bd0d @lanwin FindAndModify add fields and upsert.
lanwin authored
238 {"new", returnNew},
6397a0f @lanwin More find and modify improvments.
lanwin authored
239 {"remove", remove},
e09bd0d @lanwin FindAndModify add fields and upsert.
lanwin authored
240 {"upsert", upsert}
9ce6e59 @lanwin Move Hash function to its own class.
lanwin authored
241 };
242
6397a0f @lanwin More find and modify improvments.
lanwin authored
243 if(sort != null)
244 command.Add("sort", sort);
245 if(fields != null)
246 command.Add("fields", fields);
247
e543727 @craiggwilson cleaning up a little
craiggwilson authored
248 var response = _connection.SendCommand<FindAndModifyResult<T>>(_configuration.SerializationFactory,
249 DatabaseName,
250 typeof(T),
9ce6e59 @lanwin Move Hash function to its own class.
lanwin authored
251 command);
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
252
253 return response.Value;
254 }
255 catch(MongoCommandException)
256 {
257 // This is when there is no document to operate on
258 return null;
259 }
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
260 }
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
261
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
262 /// <summary>
263 /// Entrypoint into executing a map/reduce query against the collection.
264 /// </summary>
265 /// <returns>A <see cref="MapReduce"/></returns>
266 public MapReduce MapReduce(){
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
267 return new MapReduce(Database, Name, typeof(T));
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
268 }
269
270 ///<summary>
271 /// Count all items in the collection.
272 ///</summary>
273 public long Count(){
274 return Count(new Document());
275 }
276
277 /// <summary>
278 /// Count all items in a collection that match the query spec.
279 /// </summary>
280 /// <param name="spec">The spec.</param>
281 /// <returns></returns>
282 /// <remarks>
283 /// It will return 0 if the collection doesn't exist yet.
284 /// </remarks>
285 public long Count(object spec){
286 try {
3f2f7cd @lanwin Pass root type to send command when using count.
lanwin authored
287 var response = Database.SendCommand(typeof(T),new Document().Add("count", Name).Add("query", spec));
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
288 return Convert.ToInt64((double)response["n"]);
289 } catch (MongoCommandException) {
290 //FIXME This is an exception condition when the namespace is missing.
291 //-1 might be better here but the console returns 0.
292 return 0;
293 }
294 }
295
296 /// <summary>
297 /// Inserts the Document into the collection.
298 /// </summary>
299 public void Insert(object document, bool safemode){
300 Insert(document);
301 CheckError(safemode);
302 }
303
304 /// <summary>
305 /// Inserts the specified doc.
306 /// </summary>
307 /// <param name="document">The doc.</param>
308 public void Insert(object document){
309 Insert(new[] { document });
310 }
311
312 /// <summary>
2243bc6 @lanwin Fix bug where Document is catched by Insert<TElement>(IEnumerable<TEl…
lanwin authored
313 /// Inserts all.
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
314 /// </summary>
2243bc6 @lanwin Fix bug where Document is catched by Insert<TElement>(IEnumerable<TEl…
lanwin authored
315 /// <typeparam name="TElement">The type of the element.</typeparam>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
316 /// <param name="documents">The documents.</param>
317 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
318 public void Insert<TElement>(IEnumerable<TElement> documents, bool safemode){
319 if (safemode)
320 Database.ResetError();
321 Insert(documents);
322 CheckPreviousError(safemode);
323 }
324
325 /// <summary>
326 /// Inserts the specified documents.
327 /// </summary>
328 /// <param name="documents">The documents.</param>
329 public void Insert<TElement>(IEnumerable<TElement> documents){
2243bc6 @lanwin Fix bug where Document is catched by Insert<TElement>(IEnumerable<TEl…
lanwin authored
330 if(documents is Document)
331 {
332 Insert(new[]{(Document)documents});
333 return;
334 }
335
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
336 var rootType = typeof(T);
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
337 var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(rootType);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
338
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
339 var insertMessage = new InsertMessage(writerSettings)
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
340 {
341 FullCollectionName = FullName
342 };
343
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
344 var descriptor = _configuration.SerializationFactory.GetObjectDescriptor(rootType);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
345 var insertDocument = new List<object>();
346
347 foreach (var document in documents) {
348 var id = descriptor.GetPropertyValue(document, "_id");
349
350 if (id == null)
351 descriptor.SetPropertyValue(document, "_id", descriptor.GenerateId(document));
352
353 insertDocument.Add(document);
354 }
355
356 insertMessage.Documents = insertDocument.ToArray();
357
358 try {
8f24af5 @lanwin Fix auth bug where auth only was done when a command was send instead…
lanwin authored
359 _connection.SendMessage(insertMessage,DatabaseName);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
360 } catch (IOException exception) {
5058c2f @lanwin Rename MongoCommException to MongoConnectionException.
lanwin authored
361 throw new MongoConnectionException("Could not insert document, communication failure", _connection, exception);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
362 }
363 }
364
365 /// <summary>
366 /// Deletes documents from the collection according to the spec.
367 /// </summary>
368 /// <param name="selector">The selector.</param>
369 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
370 /// <remarks>
371 /// An empty document will match all documents in the collection and effectively truncate it.
372 /// </remarks>
fd139f9 @lanwin Add collection.Remove and mare collection.Delete as obsolete because …
lanwin authored
373 [Obsolete("Use Remove instead")]
374 public void Delete(object selector, bool safemode)
375 {
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
376 Delete(selector);
377 CheckError(safemode);
378 }
379
380 /// <summary>
fd139f9 @lanwin Add collection.Remove and mare collection.Delete as obsolete because …
lanwin authored
381 /// Remove documents from the collection according to the selector.
382 /// </summary>
383 /// <param name="selector">The selector.</param>
384 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
385 /// <remarks>
386 /// An empty document will match all documents in the collection and effectively truncate it.
387 /// See the safemode description in the class description
388 /// </remarks>
389 public void Remove(object selector, bool safemode){
390 Remove(selector);
391 CheckError(safemode);
392 }
393
394 /// <summary>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
395 /// Deletes documents from the collection according to the spec.
396 /// </summary>
397 /// <param name="selector">The selector.</param>
398 /// <remarks>
399 /// An empty document will match all documents in the collection and effectively truncate it.
400 /// </remarks>
fd139f9 @lanwin Add collection.Remove and mare collection.Delete as obsolete because …
lanwin authored
401 [Obsolete("Use Remove instead")]
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
402 public void Delete(object selector){
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
403 var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(typeof(T));
404
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
405 try {
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
406 _connection.SendMessage(new DeleteMessage(writerSettings)
407 {
408 FullCollectionName = FullName,
409 Selector = selector
8f24af5 @lanwin Fix auth bug where auth only was done when a command was send instead…
lanwin authored
410 },DatabaseName);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
411 } catch (IOException exception) {
5058c2f @lanwin Rename MongoCommException to MongoConnectionException.
lanwin authored
412 throw new MongoConnectionException("Could not delete document, communication failure", _connection, exception);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
413 }
414 }
415
416 /// <summary>
fd139f9 @lanwin Add collection.Remove and mare collection.Delete as obsolete because …
lanwin authored
417 /// Remove documents from the collection according to the selector.
418 /// </summary>
419 /// <param name="selector">The selector.</param>
420 /// <remarks>
421 /// An empty document will match all documents in the collection and effectively truncate it.
422 /// </remarks>
423 public void Remove(object selector){
424 var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(typeof(T));
425
426 try
427 {
428 _connection.SendMessage(new DeleteMessage(writerSettings)
429 {
430 FullCollectionName = FullName,
431 Selector = selector
8f24af5 @lanwin Fix auth bug where auth only was done when a command was send instead…
lanwin authored
432 }, DatabaseName);
fd139f9 @lanwin Add collection.Remove and mare collection.Delete as obsolete because …
lanwin authored
433 }
434 catch(IOException exception)
435 {
436 throw new MongoConnectionException("Could not delete document, communication failure", _connection, exception);
437 }
438 }
439
440 /// <summary>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
441 /// Updates the specified document.
442 /// </summary>
443 /// <param name="document">The document.</param>
444 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
34950dc @lanwin Add some obsolete attribute.
lanwin authored
445 [Obsolete("Use Save instead")]
446 public void Update(object document, bool safemode)
447 {
e0b2cf0 @samus Merged from Steve Wagner
authored
448 Save(document, safemode);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
449 }
450
451 /// <summary>
452 /// Updates a document with the data in doc as found by the selector.
453 /// </summary>
454 /// <param name="document">The document.</param>
455 /// <remarks>
456 /// _id will be used in the document to create a selector. If it isn't in
457 /// the document then it is assumed that the document is new and an upsert is sent to the database
458 /// instead.
459 /// </remarks>
460 [Obsolete("Use Save(Document)")]
461 public void Update(object document){
462 Save(document);
463 }
464
465 /// <summary>
466 /// Updates the specified document.
467 /// </summary>
468 /// <param name="document">The document.</param>
469 /// <param name="selector">The selector.</param>
470 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
471 public void Update(object document, object selector, bool safemode){
472 Update(document, selector, 0, safemode);
473 }
474
475 /// <summary>
476 /// Updates a document with the data in doc as found by the selector.
477 /// </summary>
478 /// <param name="document">The document.</param>
479 /// <param name="selector">The selector.</param>
480 public void Update(object document, object selector){
481 Update(document, selector, 0);
482 }
483
484 /// <summary>
485 /// Updates the specified document.
486 /// </summary>
487 /// <param name="document">The document.</param>
488 /// <param name="selector">The selector.</param>
489 /// <param name="flags">The flags.</param>
490 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
491 public void Update(object document, object selector, UpdateFlags flags, bool safemode){
492 Update(document, selector, flags);
493 CheckError(safemode);
494 }
495
496 /// <summary>
497 /// Updates a document with the data in doc as found by the selector.
498 /// </summary>
499 /// <param name="document">The <see cref="Document"/> to update with</param>
500 /// <param name="selector">The query spec to find the document to update.</param>
501 /// <param name="flags"><see cref="UpdateFlags"/></param>
502 public void Update(object document, object selector, UpdateFlags flags){
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
503 var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(typeof(T));
504
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
505 try {
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
506 _connection.SendMessage(new UpdateMessage(writerSettings)
507 {
508 FullCollectionName = FullName,
509 Selector = selector,
510 Document = document,
511 Flags = (int)flags
8f24af5 @lanwin Fix auth bug where auth only was done when a command was send instead…
lanwin authored
512 }, DatabaseName);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
513 } catch (IOException exception) {
5058c2f @lanwin Rename MongoCommException to MongoConnectionException.
lanwin authored
514 throw new MongoConnectionException("Could not update document, communication failure", _connection, exception);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
515 }
516 }
517
518 /// <summary>
519 /// Runs a multiple update query against the database. It will wrap any
520 /// doc with $set if the passed in doc doesn't contain any '$' ops.
521 /// </summary>
522 /// <param name="document">The document.</param>
523 /// <param name="selector">The selector.</param>
524 public void UpdateAll(object document, object selector){
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
525 Update(EnsureUpdateDocument(document), selector, UpdateFlags.MultiUpdate);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
526 }
527
528 /// <summary>
529 /// Updates all.
530 /// </summary>
531 /// <param name="document">The document.</param>
532 /// <param name="selector">The selector.</param>
533 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
534 public void UpdateAll(object document, object selector, bool safemode){
535 if (safemode)
536 Database.ResetError();
537 UpdateAll(document, selector);
538 CheckPreviousError(safemode);
539 }
540
541 /// <summary>
542 /// Saves a document to the database using an upsert.
543 /// </summary>
544 /// <param name="document">The document.</param>
545 /// <remarks>
546 /// The document will contain the _id that is saved to the database. This is really just an alias
547 /// to Update(Document) to maintain consistency between drivers.
548 /// </remarks>
549 public void Save(object document){
550 //Try to generate a selector using _id for an existing document.
551 //otherwise just set the upsert flag to 1 to insert and send onward.
552
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
553 var descriptor = _configuration.SerializationFactory.GetObjectDescriptor(typeof(T));
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
554
555 var value = descriptor.GetPropertyValue(document, "_id");
556
35ece88 @lanwin Fix Save dose not work for new document with existing id.
lanwin authored
557 if(value == null)
558 {
6d718e3 @craiggwilson added instance to GenerateId method.
craiggwilson authored
559 //Likely a new document
35ece88 @lanwin Fix Save dose not work for new document with existing id.
lanwin authored
560 descriptor.SetPropertyValue(document, "_id", descriptor.GenerateId(value));
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
561
35ece88 @lanwin Fix Save dose not work for new document with existing id.
lanwin authored
562 Insert(document);
563 }
564 else
565 Update(document, new Document("_id", value), UpdateFlags.Upsert);
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
566 }
567
568 /// <summary>
569 /// Saves a document to the database using an upsert.
570 /// </summary>
571 /// <param name="document">The document.</param>
572 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
a292e29 @lanwin Use Save for Update(doc).
lanwin authored
573 /// <remarks>
574 /// The document will contain the _id that is saved to the database. This is really just an alias
575 /// to Update(Document) to maintain consistency between drivers.
576 /// </remarks>
577 public void Save(object document, bool safemode)
578 {
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
579 Save(document);
580 CheckError(safemode);
581 }
582
583 /// <summary>
584 /// Checks the error.
585 /// </summary>
586 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
587 private void CheckError(bool safemode){
588 if (!safemode)
589 return;
590
591 var lastError = Database.GetLastError();
592
593 if (ErrorTranslator.IsError(lastError))
594 throw ErrorTranslator.Translate(lastError);
595 }
596
597 /// <summary>
598 /// Checks the previous error.
599 /// </summary>
600 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
601 private void CheckPreviousError(bool safemode){
602 if (!safemode)
603 return;
604
605 var previousError = Database.GetPreviousError();
606
607 if (ErrorTranslator.IsError(previousError))
608 throw ErrorTranslator.Translate(previousError);
609 }
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
610
611 /// <summary>
612 /// Ensures the update document.
613 /// </summary>
614 /// <param name="document">The document.</param>
615 /// <returns></returns>
616 private object EnsureUpdateDocument(object document)
617 {
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
618 var descriptor = _configuration.SerializationFactory.GetObjectDescriptor(typeof(T));
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
619
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
620 var foundOp = descriptor.GetMongoPropertyNames(document)
621 .Any(name => name.IndexOf('$') == 0);
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
622
623 if(foundOp == false)
624 {
625 //wrap document in a $set.
626 return new Document().Add("$set", document);
627 }
628
629 return document;
630 }
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
631 }
632 }
Something went wrong with that request. Please try again.