lib/mongo/bulk_write.rb in mongo-2.18.1 vs lib/mongo/bulk_write.rb in mongo-2.18.2
- old
+ new
@@ -113,11 +113,12 @@
# { delete_one: { filter: { _id: 2 }}}
# ]
# )
#
# @param [ Mongo::Collection ] collection The collection.
- # @param [ Array<Hash, BSON::Document> ] requests The requests, cannot be empty.
+ # @param [ Enumerable<Hash, BSON::Document> ] requests The requests,
+ # cannot be empty.
# @param [ Hash, BSON::Document ] options The options.
#
# @since 2.1.0
def initialize(collection, requests, options = {})
@collection = collection
@@ -328,14 +329,13 @@
#
# @raise [ Error::InvalidUpdateDocument, Error::InvalidReplacementDocument,
# ArgumentError ]
# if the document is invalid.
def validate_requests!
- if @requests.empty?
- raise ArgumentError, "Bulk write requests cannot be empty"
- end
+ requests_empty = true
@requests.each do |req|
+ requests_empty = false
if op = req.keys.first
if [:update_one, :update_many].include?(op)
if doc = maybe_first(req.dig(op, :update))
if key = doc.keys&.first
unless key.to_s.start_with?("$")
@@ -357,9 +357,11 @@
end
end
end
end
end
+ end.tap do
+ raise ArgumentError, "Bulk write requests cannot be empty" if requests_empty
end
end
# If the given object is an array return the first element, otherwise
# return the given object.