Example #1
0
        /// <summary>
        /// Run the update command in MongoDB.
        /// </summary>
        /// <param name="cancellation">An optional cancellation token</param>
        public async Task <UpdateResult> ExecuteAsync(CancellationToken cancellation = default)
        {
            if (models.Count > 0)
            {
                var res = await DB.BulkUpdateAsync(models, session, db, cancellation);

                models.Clear();
                return(new UpdateResult.Acknowledged(res.MatchedCount, res.ModifiedCount, null));
            }
            else
            {
                if (filter == null)
                {
                    throw new ArgumentException("Please use Match() method first!");
                }
                if (defs.Count == 0)
                {
                    throw new ArgumentException("Please use Modify() method first!");
                }
                if (stages.Count > 0)
                {
                    throw new ArgumentException("Regular updates and Pipeline updates cannot be used together!");
                }

                Modify(b => b.CurrentDate(x => x.ModifiedOn));
                return(await DB.UpdateAsync(filter, Builders <T> .Update.Combine(defs), options, session, db, cancellation));
            }
        }
Example #2
0
        /// <summary>
        /// Run the update command in MongoDB.
        /// </summary>
        public async Task ExecuteAsync()
        {
            if (models.Count > 0)
            {
                await DB.BulkUpdateAsync(models, session, db);

                models.Clear();
            }
            else
            {
                if (filter == null)
                {
                    throw new ArgumentException("Please use Match() method first!");
                }
                if (defs.Count == 0)
                {
                    throw new ArgumentException("Please use Modify() method first!");
                }
                if (stages.Count > 0)
                {
                    throw new ArgumentException("Regular updates and Pipeline updates cannot be used together!");
                }

                Modify(b => b.CurrentDate(x => x.ModifiedOn));
                await DB.UpdateAsync(filter, Builders <T> .Update.Combine(defs), options, session, db);
            }
        }
Example #3
0
        /// <summary>
        /// Run the update command in MongoDB.
        /// </summary>
        /// <param name="cancellation">An optional cancellation token</param>
        public async Task <UpdateResult> ExecuteAsync(CancellationToken cancellation = default)
        {
            if (models.Count > 0)
            {
                var bulkWriteResult = await DB.BulkUpdateAsync(models, session, cancellation).ConfigureAwait(false);

                models.Clear();
                return(new UpdateResult.Acknowledged(bulkWriteResult.MatchedCount, bulkWriteResult.ModifiedCount, null));
            }
            else
            {
                if (filter == Builders <T> .Filter.Empty)
                {
                    throw new ArgumentException("Please use Match() method first!");
                }
                if (defs.Count == 0)
                {
                    throw new ArgumentException("Please use Modify() method first!");
                }
                if (stages.Count > 0)
                {
                    throw new ArgumentException("Regular updates and Pipeline updates cannot be used together!");
                }
                if (Cache <T> .HasModifiedOn)
                {
                    Modify(b => b.CurrentDate(Cache <T> .ModifiedOnPropName));
                }

                return(await DB.UpdateAsync(filter, Builders <T> .Update.Combine(defs), options, session, cancellation).ConfigureAwait(false));
            }
        }