Exemplo n.º 1
0
            /// <summary>
            /// Finds the first document in the collection that satisfies the query criteria.
            /// </summary>
            /// <param name="filter">
            /// A document describing the find criteria using <see href="https://docs.mongodb.com/manual/reference/operator/query/">query operators</see>.
            /// If not specified, all documents in the collection will match the request.
            /// </param>
            /// <param name="updateDocument">
            /// A document describing the update. Can only contain
            /// <see href="https://docs.mongodb.com/manual/reference/operator/update/#id1">update operator expressions</see>.
            /// </param>
            /// <param name="sort">A document describing the sort criteria. If not specified, the order of the returned documents is not guaranteed.</param>
            /// <param name="projection">
            /// A document describing the fields to return for all matching documents. If not specified, all fields are returned.
            /// </param>
            /// <param name="upsert">
            /// A boolean controlling whether the update should insert a document if no documents match the <paramref name="filter"/>.
            /// Defaults to <c>false</c>.
            /// </param>
            /// <param name="returnNewDocument">
            /// A boolean controlling whether to return the new updated document. If set to <c>false</c> the original document
            /// before the update is returned. Defaults to <c>false</c>.
            /// </param>
            /// <returns>
            /// An awaitable <see cref="Task{T}"/> representing the remote find one operation. The result of the task is the first document that matches the find criteria.
            /// </returns>
            /// <seealso href="https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/"/>
            public async Task <TDocument> FindOneAndUpdateAsync(object filter, object updateDocument, object sort = null, object projection = null, bool upsert = false, bool returnNewDocument = false)
            {
                Argument.NotNull(updateDocument, nameof(updateDocument));

                var result = await _handle.FindOneAndUpdate(filter?.ToNativeJson(), updateDocument.ToNativeJson(), FindAndModifyOptions.FindAndModify(projection, sort, upsert, returnNewDocument));

                return(result.GetValue <TDocument>());
            }