Method: Mongo::Collection#insert_many

Defined in:
lib/mongo/collection.rb

#insert_many(documents, options = {}) ⇒ Result

Insert the provided documents into the collection.

Examples:

Insert documents into the collection.

collection.insert_many([{ name: 'test' }])

Parameters:

  • documents (Enumerable<Hash>)

    The documents to insert.

  • options (Hash) (defaults to: {})

    The insert options.

Options Hash (options):

  • :bypass_document_validation (true | false)

    Whether or not to skip document level validation.

  • :comment (Object)

    A user-provided comment to attach to this command.

  • :ordered (true | false)

    Whether the operations should be executed in order.

  • :session (Session)

    The session to use for the operation.

  • :write_concern (Hash)

    The write concern options. Can be :w => Integer, :fsync => Boolean, :j => Boolean.

Returns:

  • (Result)

    The database response wrapper.

Since:

  • 2.0.0



843
844
845
846
847
848
# File 'lib/mongo/collection.rb', line 843

def insert_many(documents, options = {})
  QueryCache.clear_namespace(namespace)

  inserts = documents.map{ |doc| { :insert_one => doc }}
  bulk_write(inserts, options)
end