Hola buenas soy nuevo en esto, tengo entendido que para crear el modelo de un esquema es asi=>
var Movie = mongoose.model('Movie', movieSchema);
var movie = new Movie(); //instanciamos mi objeto user(Documents are instances of our model)
movie.name = 'admin1';
movie.email = '[email protected]';
movie.password = 'Berna123';
movie.age = 28;
Pero claro yo quiero hacerlo asi pero no me deja ya que pone que Movie ya esta definido, querria despues insertar peliculas aleatoriamente con un loop, como lo puedo hacer? =>
class Movie{
Constructor(name, email, password, age){
this._name = name;
this._email = email;
this._password = password;
this._age = age;
}
}
var movieName = new Movie('Harry Potter', '[email protected]', 'Berna123', 28);
'use strict';
//1. Dependencies & Connecting
var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
mongoose.connect("mongodb://localhost/peliculas-random", function(err){
if (err) console.log('error');
else console.log('connection has been made');
});
//2. General Schema
var Schema = mongoose.Schema;
//3. Movie Schema
var movieSchema = new Schema({
name:{type:String, required:true, unique: false},
lastname:{type:String},
email:{type:String},
password:{type:String},
age:{type:Number},
});
//Mongoose Paginate
movieSchema.plugin(mongoosePaginate);
//4. Constructor of my Schema
var Movie = mongoose.model('Movie', movieSchema);
//5. Populating Collection
function createMovie(){
var movie = new Movie(); //instanciamos mi objeto user(Documents are instances of our model)
movie.name = 'admin1';
movie.email = '[email protected]';
movie.password = 'Berna123';
movie.age = 28;
movie.save(function(err){ // Sving instance of my model
if (err) console.log('There is an error: ' + err.message);
else console.log('Movie created successfully');
});
console.log(movie);
}
createMovie();
// class Movie{
// Constructor(name, email, password, age){
// this._name = name;
// this._email = email;
// this._password = password;
// this._age = age;
// }
// }
//
// var movieName = new Movie('Harry Potter', '[email protected]', 'Berna123', 28);
//
//
// mongoose.connection.close();