using System; using System.Collections.Generic; using System.Linq; using System.Text; using MongoDB.Linq; using System.Threading.Tasks; using System.Threading; namespace MongoDB { public partial class MongoCollection : IMongoCollection { /// /// Finds the first document in a selector query and returns it through the callback delegate. /// /// The where. /// The callback action. /// A for cancelling the task. public CancellationTokenSource FindOneAsync(string javascriptWhere, Action returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = FindOne(javascriptWhere); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } },cancel.Token); return cancel; } /// /// Finds the first document in a selector query and returns it through the callback delegate. /// /// The selector. /// The callback action /// A for cancelling the task. public CancellationTokenSource FindOneAsync(object selector, Action returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = FindOne(selector); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } }, cancel.Token); return cancel; } /// /// Gets a cursor that contains all of the documents in the collection and returns the cursor through the callback action. /// /// The callback delegate /// /// Cursors load documents from the database in batches instead of all at once. /// /// A for cancelling the task. public CancellationTokenSource FindAllAsync(Action, Exception> returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = FindAll(); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } }, cancel.Token); return cancel; } /// /// Uses the $where operator to query the collection. The value of the where is Javascript that will /// produce a true for the documents that match the criteria. /// Returns the cursor through the callback delegate. /// /// Javascript /// The callback delegate. /// A for cancelling the task. public CancellationTokenSource FindAsync(string javascriptWhere, Action, Exception> returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = Find(javascriptWhere); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } }, cancel.Token); return cancel; } /// /// Queries the collection using the query selector and returns the cursor through the callback delegate. /// /// The selector. /// The callback delegate. /// A for cancelling the task. public CancellationTokenSource FindAsync(object selector, Action, Exception> returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = Find(selector); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } }, cancel.Token); return cancel; } /// /// Queries the collection using the specification and only returns a subset of fields through the callback delegate. /// /// The selector. /// The fields. /// The callback delegate /// A for cancelling the task. public CancellationTokenSource FindAsync(object selector, object fields, Action, Exception> returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = Find(selector, fields); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } }, cancel.Token); return cancel; } /// /// Executes a query and atomically applies a modifier operation to the first document returning the original document /// by default through the callback delegate. /// /// The document. /// The selector. /// The callback delegate. /// A for cancelling the task. public CancellationTokenSource FindAndModifyAsync(object document, object selector, Action returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = FindAndModify(document, selector); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } }, cancel.Token); return cancel; } /// /// Executes a query and atomically applies a modifier operation to the first document returning the original document /// by default through the callback delegate. /// /// The document. /// The selector. /// containing the names of columns to sort on with the values being the /// The callback delegate. /// A for cancelling the task. /// public CancellationTokenSource FindAndModifyAsync(object document, object selector, object sort, Action returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = FindAndModify(document, selector, sort); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } }, cancel.Token); return cancel; } /// /// Executes a query and atomically applies a modifier operation to the first document returning the original document /// by default through the callback delegate. /// /// The document. /// The selector. /// if set to true [return new]. /// The callback delegate. /// A for cancelling the task. public CancellationTokenSource FindAndModifyAsync(object document, object selector, bool returnNew, Action returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = FindAndModify(document, selector, returnNew); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } }, cancel.Token); return cancel; } /// /// Executes a query and atomically applies a modifier operation to the first document returning the original document /// by default through the callback delegate. /// /// The document. /// The selector. /// containing the names of columns to sort on with the values being the /// /// if set to true [return new]. /// The callback delegate. /// A for cancelling the task. public CancellationTokenSource FindAndModifyAsync(object document, object selector, object sort, bool returnNew, Action returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = FindAndModify(document, selector, sort, returnNew); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } }, cancel.Token); return cancel; } /// /// Entrypoint into executing a map/reduce query against the collection. /// /// The callback delegate. /// A for cancelling the task. public CancellationTokenSource MapReduceAsync(Action returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = MapReduce(); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(null, ex); } } }, cancel.Token); return cancel; } /// /// Count all items in the collection. /// ///The callback delegate /// A for cancelling the task. public CancellationTokenSource CountAsync(Action returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = Count(); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(0, ex); } } }, cancel.Token); return cancel; } /// /// Count all items in a collection that match the query selector. /// /// The selector. /// The callback delegate. /// A for cancelling the task. /// /// It will return 0 if the collection doesn't exist yet. /// public CancellationTokenSource CountAsync(object selector, Action returnDelegate) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { var t = Count(selector); returnDelegate.Invoke(t, null); } catch (Exception ex) { returnDelegate.Invoke(0, ex); } } }, cancel.Token); return cancel; } /// /// Inserts the Document into the collection. /// /// The doc. /// The callback delegate for handling exceptions. /// A for cancelling the task. public CancellationTokenSource InsertAsync(object document, bool safemode, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Insert(document, safemode); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Inserts the specified doc. /// /// The doc. /// The callback delegate for handling exceptions. /// A for cancelling the task. public CancellationTokenSource InsertAsync(object document, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Insert(document); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Bulk inserts the specified documents into the database. /// /// The docs. /// The callback delegate for handling exceptions. /// /// See the safemode description in the class description /// /// A for cancelling the task. public CancellationTokenSource InsertAsync(IEnumerable documents, bool safemode, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Insert(documents, safemode); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Bulk inserts the specified documents into the database. /// /// The documents. /// The callback delegate for handling exceptions. /// A for cancelling the task. public CancellationTokenSource InsertAsync(IEnumerable documents, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Insert(documents); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Remove documents from the collection according to the selector. /// /// The selector. /// if set to true [safemode]. /// The callback delegate for handling exceptions. /// /// An empty document will match all documents in the collection and effectively truncate it. /// See the safemode description in the class description /// /// A for cancelling the task. public CancellationTokenSource RemoveAsync(object selector, bool safemode, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Remove(selector, safemode); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Remove documents from the collection according to the selector. /// /// The callback delegate for handling exceptions. /// The selector. /// /// An empty document will match all documents in the collection and effectively truncate it. /// /// A for cancelling the task. public CancellationTokenSource RemoveAsync(object selector, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Remove(selector); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Updates the specified document with the current document. In order to only do a partial update use a /// document containing modifier operations ($set, $unset, $inc, etc.) /// /// The document. /// The selector. /// if set to true [safemode]. /// The callback delegate for handling exceptions. /// /// See the safemode description in the class description /// /// A for cancelling the task. public CancellationTokenSource UpdateAsync(object document, object selector, bool safemode, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Update(document, selector, safemode); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Updates the specified document with the current document. In order to only do a partial update use a /// document containing modifier operations ($set, $unset, $inc, etc.) /// /// The document. /// The selector. /// The callback delegate for handling exceptions. /// A for cancelling the task. public CancellationTokenSource UpdateAsync(object document, object selector, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Update(document, selector); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Updates the specified document with the current document. In order to only do a partial update use a /// document containing modifier operations ($set, $unset, $inc, etc.) /// /// The document. /// The selector. /// The flags. /// if set to true [safemode]. /// The callback delegate to handle exceptions. /// /// See the safemode description in the class description /// /// A for cancelling the task. public CancellationTokenSource UpdateAsync(object document, object selector, UpdateFlags flags, bool safemode, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Update(document, selector, flags, safemode); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Updates the specified document with the current document. In order to only do a partial update use a /// document containing modifier operations ($set, $unset, $inc, etc.) /// /// The to update with /// The query selector to find the document to update. /// /// The callback delegate for handling exceptions. /// A for cancelling the task. public CancellationTokenSource UpdateAsync(object document, object selector, UpdateFlags flags, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Update(document, selector, flags); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Runs a multiple update query against the database. It will wrap any /// doc with $set if the passed in doc doesn't contain any '$' modifier ops. /// /// The document. /// The selector. /// The callback delegate for handling exceptions. /// A for cancelling the task. public CancellationTokenSource UpdateAllAsync(object document, object selector, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { UpdateAll(document, selector); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Runs a multiple update query against the database. It will wrap any /// doc with $set if the passed in doc doesn't contain any '$' modifier ops. /// /// The document. /// The selector. /// if set to true [safemode]. /// The callback delegate for handling exceptions. /// /// See the safemode description in the class description /// /// A for cancelling the task. public CancellationTokenSource UpdateAllAsync(object document, object selector, bool safemode, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { UpdateAll(document, selector, safemode); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Inserts or updates a document in the database. If the document does not contain an _id one will be /// generated and an upsert sent. Otherwise the document matching the _id of the document will be updated. /// /// The document. /// The callback delegate for handling exceptions. /// /// The document will contain the _id that is saved to the database. /// /// A for cancelling the task. public CancellationTokenSource SaveAsync(object document, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Save(document); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } /// /// Saves a document to the database using an upsert. /// /// The document. /// if set to true [safemode]. /// The callback delegate for handling exceptions. /// A for cancelling the task. public CancellationTokenSource SaveAsync(object document, bool safemode, Action exceptionHandler) { var cancel = new CancellationTokenSource(); new TaskFactory().StartNew(() => { if (!cancel.Token.IsCancellationRequested) { try { Save(document, safemode); exceptionHandler.Invoke(null); } catch (Exception ex) { exceptionHandler.Invoke(ex); } } }, cancel.Token); return cancel; } } }