## Insert a Document 

## Step 1: Insert a document into a collection. 

Insert a document into a collection named inventory . The operation will create the collection if the collection does not currently exist. 

db.inventory.insert( { item: "ABC1", details: { model: "14Q3", manufacturer: "XYZ Company" }, stock: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ], category: "clothing" } ) 

The operation returns a WriteResult object with the status of the operation. A successful insert of the document returns the following object: 

WriteResult({ "nInserted" : 1 }) 

The nInserted field specifies the number of documents inserted. If the operation encounters an error, the WriteResult object will contain the error information. 

## Step 2: Review the inserted document. 

If the insert operation is successful, verify the insertion by querying the collection. 

db.inventory.find() 

The document you inserted should return. 

{ "_id" : ObjectId("53d98f133bb604791249ca99"), "item" : "ABC1", "details" : { "model" : 

The returned document shows that MongoDB added an _id field to the document. If a client inserts a document that does not contain the _id field, MongoDB adds the field with the value set to a generated ObjectId$^{9}$. The ObjectId 10 values in your documents will differ from the ones shown. 

## Insert an Array of Documents 

You can pass an array of documents to the db.collection.insert() method to insert multiple documents. 

## Step 1: Create an array of documents. 

Define a variable mydocuments that holds an array of documents to insert. 

var mydocuments = [ { 

