Morris
Morris

Reputation: 601

MongoDB and mongoose

I studing NodeJs and for the first time , i must used mongoose package for insert data into MongoDB. Into mongoose, what are "Schema" and "Module"? I don't really understand follow code:

 var Schema = mongoose.Schema;

var personSchema = new Schema ({ 
       Firstname: String,
       Lastname: String
});

var Person = mongoose.model('Person', personSchema);

var person1 = Person {(
       Firstname: "...",
       Lastname: "..."

)};

Thanks all

Upvotes: 1

Views: 47

Answers (1)

Mikhail Bobrovsky
Mikhail Bobrovsky

Reputation: 86

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var personSchema = new Schema ({ 
   firstname: {type: String, required: true},
   lastname: {type: String, required: true}
});

To use our schema definition, we need to convert our personSchema into a Model we can work with. To do so, we pass it into:

var Person = mongoose.model('Person', personSchema);

To more information enter link description here

Upvotes: 1

Related Questions