MongoDB — Creating & Inserting Elements Using Mongoose

Creating an element to be inserted into a database is a two step process.

  1. Create a Schema
var itemSchema = new mongoose.Schema({
param1: value,
param2: value2,
param3: value3
});

2. Create a Model

var Item = mongoose.model("Item", itemSchema);

3. Create the Element

Item.create({
param1: value,
param2: value,
param3: value
});

4. Create automatically pushes the element to the database

In essence, the Schema is created as a template as to how the data is going to be stored. What values are going to be expected, etc.

The Model is then created, using the Schema as the main object by which you can call functions such as create, find, etc.