protected long UpdateAll(QueryOptions options, IMongoUpdate update, bool sendNotifications = true)
        {
            var result = _collection.Update(options.GetQuery(_getIdValue), update, UpdateFlags.Multi);

            if (!sendNotifications || !EnableNotifications || _messagePublisher == null)
            {
                return(result.DocumentsAffected);
            }

            if (options.OrganizationIds.Any())
            {
                foreach (var orgId in options.OrganizationIds)
                {
                    PublishMessageAsync(new EntityChange {
                        ChangeType     = EntityChangeType.UpdatedAll,
                        OrganizationId = orgId,
                        Type           = _entityType
                    });
                }
            }
            else
            {
                PublishMessageAsync(new EntityChange {
                    ChangeType = EntityChangeType.UpdatedAll,
                    Type       = _entityType
                });
            }

            return(result.DocumentsAffected);
        }
        protected long RemoveAll(QueryOptions options, bool sendNotifications = true)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            var fields = new List <string>(new[] { CommonFieldNames.Id });

            if (_isOwnedByOrganization)
            {
                fields.Add(CommonFieldNames.OrganizationId);
            }
            if (_isOwnedByProject)
            {
                fields.Add(CommonFieldNames.ProjectId);
            }
            if (_isOwnedByStack)
            {
                fields.Add(CommonFieldNames.StackId);
            }

            long recordsAffected = 0;

            var documents = Collection.FindAs <T>(options.GetQuery(_getIdValue))
                            .SetLimit(RepositoryConstants.BATCH_SIZE)
                            .SetFields(fields.ToArray())
                            .ToList();

            while (documents.Count > 0)
            {
                recordsAffected += documents.Count;
                Remove(documents, sendNotifications);

                documents = Collection.FindAs <T>(options.GetQuery(_getIdValue))
                            .SetLimit(RepositoryConstants.BATCH_SIZE)
                            .SetFields(fields.ToArray())
                            .ToList();
            }

            return(recordsAffected);
        }