Method: Mongo::Collection#insert

Defined in:
lib/mongo/collection.rb

#insert(doc_or_docs, options = {}) ⇒ ObjectID, Array Also known as: <<

Insert one or more documents into the collection.

Parameters:

  • doc_or_docs (Hash, Array)

    a document (as a hash) or array of documents to be inserted.

  • opts (Hash)

    a customizable set of options

Returns:

  • (ObjectID, Array)

    the _id of the inserted document or a list of _ids of all inserted documents. Note: the object may have been modified by the database’s PK factory, if it has one.



256
257
258
259
260
261
# File 'lib/mongo/collection.rb', line 256

def insert(doc_or_docs, options={})
  doc_or_docs = [doc_or_docs] unless doc_or_docs.is_a?(Array)
  doc_or_docs.collect! { |doc| @pk_factory.create_pk(doc) }
  result = insert_documents(doc_or_docs, @name, true, options[:safe])
  result.size > 1 ? result : result.first
end