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 (Array<Hash>)

    The documents to insert.

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

    The insert options.

Options Hash (options):

  • :ordered (true | false)

    Whether the operations should be executed in order.

  • :session (Session)

    The session to use for the operation.

Returns:

  • (Result)

    The database response wrapper.

Since:

  • 2.0.0


631
632
633
634
635
636
# File 'lib/mongo/collection.rb', line 631

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

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