Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 607 lines (539 sloc) 24.113 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;
4a0cc9b @lanwin Fix some warnings.
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>
29 /// <param name="name">The name.</param>
391fe44 @lanwin Merge with craig.
lanwin authored
30 public MongoCollection(MongoConfiguration configuration, Connection connection, string databaseName, string name)
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
31 {
32 //Todo: This should be internal
33 Name = name;
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>
4a0cc9b @lanwin Fix some warnings.
lanwin authored
71 public CollectionMetadata MetaData {
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
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();
8f24af5 @lanwin Fix auth bug where auth only was done when a command was send instead…
lanwin authored
161 return new Cursor<T>(_configuration.SerializationFactory, _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"/>
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
184 public T FindAndModify(object document, object spec, object sort){
185 return FindAndModify(document, spec, sort);
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
186 }
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
187
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
188 /// <summary>
189 /// Executes a query and atomically applies a modifier operation to the first document returning the original document
190 /// by default.
191 /// </summary>
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
192 /// <param name="document">The document.</param>
193 /// <param name="spec"><see cref="Document"/> to find the document.</param>
194 /// <param name="returnNew">if set to <c>true</c> [return new].</param>
195 /// <returns>A <see cref="Document"/></returns>
196 public T FindAndModify(object document, object spec, bool returnNew){
197 return FindAndModify(document, spec, new Document(), returnNew);
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
198 }
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
199
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
200 /// <summary>
201 /// Executes a query and atomically applies a modifier operation to the first document returning the original document
202 /// by default.
203 /// </summary>
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
204 /// <param name="document">The document.</param>
205 /// <param name="spec"><see cref="Document"/> to find the document.</param>
206 /// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the
207 /// <see cref="IndexOrder"/></param>
208 /// <param name="returnNew">if set to <c>true</c> [return new].</param>
209 /// <returns>A <see cref="Document"/></returns>
210 public T FindAndModify(object document, object spec, object sort, bool returnNew){
211 try
212 {
9ce6e59 @lanwin Move Hash function to its own class.
lanwin authored
213 var command = new Document
214 {
215 {"findandmodify", Name},
216 {"query", spec},
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
217 {"update", EnsureUpdateDocument(document)},
9ce6e59 @lanwin Move Hash function to its own class.
lanwin authored
218 {"sort", sort},
219 {"new", returnNew}
220 };
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
221
222 var response = _connection.SendCommand<FindAndModifyResult<T>>(_configuration.SerializationFactory,
223 DatabaseName,
224 typeof(T),
225 command);
226
227 return response.Value;
228 }
4d69e28 Merge branch 'master' into typedcollections
Samuel Corder authored
229 catch(MongoCommandException)
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
230 {
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
231 // This is when there is no document to operate on
232 return null;
233 }
234 }
235
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
236 /// <summary>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
237 /// Entrypoint into executing a map/reduce query against the collection.
238 /// </summary>
239 /// <returns>A <see cref="MapReduce"/></returns>
240 public MapReduce MapReduce(){
241 return new MapReduce(Database, Name, typeof(T));
242 }
243
244 ///<summary>
245 /// Count all items in the collection.
246 ///</summary>
247 public long Count(){
248 return Count(new Document());
249 }
250
251 /// <summary>
252 /// Count all items in a collection that match the query spec.
253 /// </summary>
254 /// <param name="spec">The spec.</param>
255 /// <returns></returns>
3f2f7cd @lanwin Pass root type to send command when using count.
lanwin authored
256 /// <remarks>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
257 /// It will return 0 if the collection doesn't exist yet.
258 /// </remarks>
259 public long Count(object spec){
260 try {
261 var response = Database.SendCommand(typeof(T),new Document().Add("count", Name).Add("query", spec));
262 return Convert.ToInt64((double)response["n"]);
263 } catch (MongoCommandException) {
264 //FIXME This is an exception condition when the namespace is missing.
265 //-1 might be better here but the console returns 0.
266 return 0;
267 }
268 }
269
270 /// <summary>
271 /// Inserts the Document into the collection.
272 /// </summary>
273 public void Insert(object document, bool safemode){
274 Insert(document);
275 CheckError(safemode);
276 }
277
278 /// <summary>
279 /// Inserts the specified doc.
280 /// </summary>
281 /// <param name="document">The doc.</param>
2243bc6 @lanwin Fix bug where Document is catched by Insert<TElement>(IEnumerable<TEl…
lanwin authored
282 public void Insert(object document){
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
283 Insert(new[] { document });
2243bc6 @lanwin Fix bug where Document is catched by Insert<TElement>(IEnumerable<TEl…
lanwin authored
284 }
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
285
286 /// <summary>
287 /// Inserts all.
288 /// </summary>
289 /// <typeparam name="TElement">The type of the element.</typeparam>
290 /// <param name="documents">The documents.</param>
291 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
292 public void Insert<TElement>(IEnumerable<TElement> documents, bool safemode){
293 if (safemode)
294 Database.ResetError();
295 Insert(documents);
296 CheckPreviousError(safemode);
297 }
298
2243bc6 @lanwin Fix bug where Document is catched by Insert<TElement>(IEnumerable<TEl…
lanwin authored
299 /// <summary>
300 /// Inserts the specified documents.
301 /// </summary>
302 /// <param name="documents">The documents.</param>
303 public void Insert<TElement>(IEnumerable<TElement> documents){
304 if(documents is Document)
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
305 {
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
306 Insert(new[]{(Document)documents});
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
307 return;
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
308 }
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
309
310 var rootType = typeof(T);
311 var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(rootType);
312
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
313 var insertMessage = new InsertMessage(writerSettings)
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
314 {
315 FullCollectionName = FullName
316 };
317
318 var descriptor = _configuration.SerializationFactory.GetObjectDescriptor(rootType);
319 var insertDocument = new List<object>();
320
321 foreach (var document in documents) {
322 var id = descriptor.GetPropertyValue(document, "_id");
323
324 if (id == null)
325 descriptor.SetPropertyValue(document, "_id", descriptor.GenerateId(document));
326
327 insertDocument.Add(document);
8f24af5 @lanwin Fix auth bug where auth only was done when a command was send instead…
lanwin authored
328 }
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
329
5058c2f @lanwin Rename MongoCommException to MongoConnectionException.
lanwin authored
330 insertMessage.Documents = insertDocument.ToArray();
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
331
332 try {
333 _connection.SendMessage(insertMessage,DatabaseName);
334 } catch (IOException exception) {
335 throw new MongoConnectionException("Could not insert document, communication failure", _connection, exception);
336 }
337 }
338
339 /// <summary>
340 /// Deletes documents from the collection according to the spec.
341 /// </summary>
fd139f9 @lanwin Add collection.Remove and mare collection.Delete as obsolete because …
lanwin authored
342 /// <param name="selector">The selector.</param>
343 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
344 /// <remarks>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
345 /// An empty document will match all documents in the collection and effectively truncate it.
346 /// </remarks>
347 [Obsolete("Use Remove instead")]
348 public void Delete(object selector, bool safemode)
349 {
fd139f9 @lanwin Add collection.Remove and mare collection.Delete as obsolete because …
lanwin authored
350 Delete(selector);
351 CheckError(safemode);
352 }
353
354 /// <summary>
355 /// Remove documents from the collection according to the selector.
356 /// </summary>
357 /// <param name="selector">The selector.</param>
358 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
359 /// <remarks>
360 /// An empty document will match all documents in the collection and effectively truncate it.
361 /// See the safemode description in the class description
362 /// </remarks>
363 public void Remove(object selector, bool safemode){
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
364 Remove(selector);
365 CheckError(safemode);
366 }
367
368 /// <summary>
369 /// Deletes documents from the collection according to the spec.
fd139f9 @lanwin Add collection.Remove and mare collection.Delete as obsolete because …
lanwin authored
370 /// </summary>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
371 /// <param name="selector">The selector.</param>
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
372 /// <remarks>
373 /// An empty document will match all documents in the collection and effectively truncate it.
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
374 /// </remarks>
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
375 [Obsolete("Use Remove instead")]
376 public void Delete(object selector){
377 var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(typeof(T));
378
8f24af5 @lanwin Fix auth bug where auth only was done when a command was send instead…
lanwin authored
379 try {
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
380 _connection.SendMessage(new DeleteMessage(writerSettings)
5058c2f @lanwin Rename MongoCommException to MongoConnectionException.
lanwin authored
381 {
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
382 FullCollectionName = FullName,
383 Selector = selector
384 },DatabaseName);
385 } catch (IOException exception) {
fd139f9 @lanwin Add collection.Remove and mare collection.Delete as obsolete because …
lanwin authored
386 throw new MongoConnectionException("Could not delete document, communication failure", _connection, exception);
387 }
388 }
389
390 /// <summary>
391 /// Remove documents from the collection according to the selector.
392 /// </summary>
393 /// <param name="selector">The selector.</param>
394 /// <remarks>
395 /// An empty document will match all documents in the collection and effectively truncate it.
396 /// </remarks>
397 public void Remove(object selector){
398 var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(typeof(T));
399
400 try
8f24af5 @lanwin Fix auth bug where auth only was done when a command was send instead…
lanwin authored
401 {
fd139f9 @lanwin Add collection.Remove and mare collection.Delete as obsolete because …
lanwin authored
402 _connection.SendMessage(new DeleteMessage(writerSettings)
403 {
404 FullCollectionName = FullName,
405 Selector = selector
406 }, DatabaseName);
407 }
408 catch(IOException exception)
409 {
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
410 throw new MongoConnectionException("Could not delete document, communication failure", _connection, exception);
411 }
412 }
413
34950dc @lanwin Add some obsolete attribute.
lanwin authored
414 /// <summary>
415 /// Updates the specified document.
416 /// </summary>
e0b2cf0 @samus Merged from Steve Wagner
authored
417 /// <param name="document">The document.</param>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
418 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
419 [Obsolete("Use Save instead")]
420 public void Update(object document, bool safemode)
421 {
422 Save(document, safemode);
423 }
424
425 /// <summary>
426 /// Updates a document with the data in doc as found by the selector.
427 /// </summary>
428 /// <param name="document">The document.</param>
429 /// <remarks>
430 /// _id will be used in the document to create a selector. If it isn't in
431 /// the document then it is assumed that the document is new and an upsert is sent to the database
432 /// instead.
433 /// </remarks>
434 [Obsolete("Use Save(Document)")]
435 public void Update(object document){
436 Save(document);
437 }
438
439 /// <summary>
440 /// Updates the specified document.
441 /// </summary>
442 /// <param name="document">The document.</param>
443 /// <param name="selector">The selector.</param>
444 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
445 public void Update(object document, object selector, bool safemode){
446 Update(document, selector, 0, safemode);
447 }
448
449 /// <summary>
450 /// Updates a document with the data in doc as found by the selector.
451 /// </summary>
452 /// <param name="document">The document.</param>
453 /// <param name="selector">The selector.</param>
454 public void Update(object document, object selector){
455 Update(document, selector, 0);
456 }
457
458 /// <summary>
459 /// Updates the specified document.
460 /// </summary>
461 /// <param name="document">The document.</param>
462 /// <param name="selector">The selector.</param>
463 /// <param name="flags">The flags.</param>
464 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
465 public void Update(object document, object selector, UpdateFlags flags, bool safemode){
466 Update(document, selector, flags);
467 CheckError(safemode);
468 }
469
470 /// <summary>
471 /// Updates a document with the data in doc as found by the selector.
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
472 /// </summary>
473 /// <param name="document">The <see cref="Document"/> to update with</param>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
474 /// <param name="selector">The query spec to find the document to update.</param>
2f438f1 @lanwin Refactored ISerializationFactory to use GetBsonWriterSettings instead…
lanwin authored
475 /// <param name="flags"><see cref="UpdateFlags"/></param>
476 public void Update(object document, object selector, UpdateFlags flags){
477 var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(typeof(T));
478
479 try {
480 _connection.SendMessage(new UpdateMessage(writerSettings)
8f24af5 @lanwin Fix auth bug where auth only was done when a command was send instead…
lanwin authored
481 {
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
482 FullCollectionName = FullName,
5058c2f @lanwin Rename MongoCommException to MongoConnectionException.
lanwin authored
483 Selector = selector,
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
484 Document = document,
485 Flags = (int)flags
486 }, DatabaseName);
487 } catch (IOException exception) {
488 throw new MongoConnectionException("Could not update document, communication failure", _connection, exception);
489 }
490 }
491
492 /// <summary>
493 /// Runs a multiple update query against the database. It will wrap any
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
494 /// doc with $set if the passed in doc doesn't contain any '$' ops.
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
495 /// </summary>
496 /// <param name="document">The document.</param>
497 /// <param name="selector">The selector.</param>
498 public void UpdateAll(object document, object selector){
499 Update(EnsureUpdateDocument(document), selector, UpdateFlags.MultiUpdate);
500 }
501
502 /// <summary>
503 /// Updates all.
504 /// </summary>
505 /// <param name="document">The document.</param>
506 /// <param name="selector">The selector.</param>
507 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
508 public void UpdateAll(object document, object selector, bool safemode){
509 if (safemode)
510 Database.ResetError();
511 UpdateAll(document, selector);
512 CheckPreviousError(safemode);
513 }
514
515 /// <summary>
516 /// Saves a document to the database using an upsert.
517 /// </summary>
518 /// <param name="document">The document.</param>
519 /// <remarks>
520 /// The document will contain the _id that is saved to the database. This is really just an alias
521 /// to Update(Document) to maintain consistency between drivers.
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
522 /// </remarks>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
523 public void Save(object document){
524 //Try to generate a selector using _id for an existing document.
525 //otherwise just set the upsert flag to 1 to insert and send onward.
35ece88 @lanwin Fix Save dose not work for new document with existing id.
lanwin authored
526
527 var descriptor = _configuration.SerializationFactory.GetObjectDescriptor(typeof(T));
6d718e3 @craiggwilson added instance to GenerateId method.
craiggwilson authored
528
35ece88 @lanwin Fix Save dose not work for new document with existing id.
lanwin authored
529 var value = descriptor.GetPropertyValue(document, "_id");
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
530
35ece88 @lanwin Fix Save dose not work for new document with existing id.
lanwin authored
531 if(value == null)
532 {
533 //Likely a new document
534 descriptor.SetPropertyValue(document, "_id", descriptor.GenerateId(value));
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
535
536 Insert(document);
537 }
538 else
539 Update(document, new Document("_id", value), UpdateFlags.Upsert);
540 }
541
a292e29 @lanwin Use Save for Update(doc).
lanwin authored
542 /// <summary>
543 /// Saves a document to the database using an upsert.
544 /// </summary>
545 /// <param name="document">The document.</param>
546 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
547 /// <remarks>
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
548 /// The document will contain the _id that is saved to the database. This is really just an alias
549 /// to Update(Document) to maintain consistency between drivers.
550 /// </remarks>
551 public void Save(object document, bool safemode)
552 {
553 Save(document);
554 CheckError(safemode);
555 }
556
557 /// <summary>
558 /// Checks the error.
559 /// </summary>
560 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
561 private void CheckError(bool safemode){
562 if (!safemode)
563 return;
564
565 var lastError = Database.GetLastError();
566
567 if (ErrorTranslator.IsError(lastError))
568 throw ErrorTranslator.Translate(lastError);
569 }
570
571 /// <summary>
572 /// Checks the previous error.
573 /// </summary>
574 /// <param name="safemode">if set to <c>true</c> [safemode].</param>
575 private void CheckPreviousError(bool safemode){
576 if (!safemode)
577 return;
578
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
579 var previousError = Database.GetPreviousError();
580
581 if (ErrorTranslator.IsError(previousError))
582 throw ErrorTranslator.Translate(previousError);
583 }
584
585 /// <summary>
586 /// Ensures the update document.
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
587 /// </summary>
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
588 /// <param name="document">The document.</param>
7c7986a @lanwin Pass MongoConfiguration down instead of ISerializationFactory.
lanwin authored
589 /// <returns></returns>
590 private object EnsureUpdateDocument(object document)
25bfc6c @lanwin Fix merge errors. Lot of test currently failing.
lanwin authored
591 {
592 var descriptor = _configuration.SerializationFactory.GetObjectDescriptor(typeof(T));
593
594 var foundOp = descriptor.GetMongoPropertyNames(document)
595 .Any(name => name.IndexOf('$') == 0);
596
597 if(foundOp == false)
598 {
599 //wrap document in a $set.
dd9bfaf @samus Mongo name put back in front of Collection and Database classes to av…
authored
600 return new Document().Add("$set", document);
601 }
Something went wrong with that request. Please try again.