// Requires a MongoDB 3.6+ instance
var mongoose = require('mongoose');
mongoose.connect('mongodb://<db_dir>/<db_name>');
var mongooseDynamic = require("mongoose-dynamic-schemas")
const util = require('util');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {});
var dogSchema = mongoose.Schema({
name: {type : String, required : true, default : "No name"},
color : {type : String, required : true, default : "No color"},
breed : {type : String, required : true, default : "No breed"},
age : {type : String, required : true, default : "5"},
children : {type : Number, required : true, default : 2},
stats : {}
});
var Dog = mongoose.model('Dogs', dogSchema);
mongoose.connection.collections['dogs'].drop( function(err) {
console.log('Collection dropped');
new Dog({ name: 'Rufo', age : "13", familyDogs : [{name : "Pancho", relation : "Son", friendship : 1 }]}).save()
.then(dogs => console.log("1 - "+util.inspect(dogs, false, null)))
.then(function() {console.log("\n------------------------------\n")})
.then(() => mongooseDynamic.addSchemaField (Dog, "family", {type : String, default : "No family"}))
.then(() => mongooseDynamic.addSchemaField (Dog, "stats.power", {type : Number, required : true, default : 50}))
.then(() => mongooseDynamic.addSchemaField (Dog, "stats.speed", {type : Number, required : true, default : 55}))
.then(() => mongooseDynamic.addSchemaField (Dog, "familyDogs", [{name : {type : String, default : "No name"}, relation : {type : String, default : "No relation"}, friendship : {type : Number, default : 0}}]))
.then(() => mongooseDynamic.addSchemaField (Dog, "familyDogs.meetings", [{mDate : {type : Date, default : Date.now}}]))
.then(() => mongooseDynamic.addSchemaField (Dog, "familyDogs.meetings.time", {type : Number, default : 10}))
.then(() => mongooseDynamic.addSchemaField (Dog, "familyDogs.meetings.location", {type : String, default : "Somewhere"}))
.then(() => new Dog({ name: 'Pancho', family : 'Gazquez', familyDogs : [{name : "Rufo", relation : "Father", friendship : 1, meetings : [{time : 15}, {mDate: new Date(2017, 9, 5, 13, 24)}] }, {name : "Bimbo", relation : "Brother" }] }).save())
.then(() => Dog.find({}).lean().exec())
.then(dogs => console.log("2 - "+util.inspect(dogs, false, null)))
.then(function() {console.log("\n------------------------------\n")})
.then(() => mongooseDynamic.removeSchemaField (Dog, "stats.speed"))
.then(() => mongooseDynamic.removeSchemaField (Dog, "familyDogs.name"))
.then(() => new Dog({ name: 'Bimbo', family : 'Gazquez', color : 'Negro', stats : {power : 20 }}).save())
.then(() => Dog.find({}).lean().exec())
.then(dogs => console.log("3 - "+util.inspect(dogs, false, null)))
.then(function() {console.log("\n------------------------------\n")})
.then(() => mongooseDynamic.moveSchemaField (Dog, "color", "looks.colour"))
.then(() => mongooseDynamic.moveSchemaField (Dog, "stats.power", "power", true))
.then(() => mongooseDynamic.moveSchemaField (Dog, "familyDogs.friendship", "familyDogs.something.love"))
.then(() => new Dog({ name: 'Lola', family : 'Gazquez' }).save())
.then(() => Dog.find({}).lean().exec())
.then(dogs => console.log("4 - "+util.inspect(dogs, false, null)))
.then(function() {console.log("\n------------------------------\n")})
.then(() => mongooseDynamic.changeFieldType (Dog, "age", Number, 1, true, true))
.then(() => mongooseDynamic.changeFieldDefinition (Dog, "breed", {field1 : {type : String, default : "Pomerania", required: true}, field2 : {type : Number, default : 6, required: true}}))
.then(() => mongooseDynamic.changeFieldDefinition (Dog, "children", {type : Boolean, default : false, required: true}))
.then(() => mongooseDynamic.changeFieldDefinition (Dog, "looks.colour", {type : Number, default : 3, required: true}))
.then(() => mongooseDynamic.changeFieldDefinition (Dog, "familyDogs.meetings.location", {type : Number, default : 2, required: true}))
.then(() => mongooseDynamic.changeFieldDefinition (Dog, "familyDogs.relation", {type : Boolean, default : true, required: true}))
.then(() => new Dog({ name: 'Wolf', family : 'Some family' , familyDogs : [{name : "Pepe", friendship : 4, hello : "nope"}]}).save())
.then(() => Dog.update({name : "Pancho"}, {"familyDogs.$[].meetings.$[].location" : 4}).exec())
.then(() => Dog.find({}).lean().exec())
.then(dogs => console.log("5 - "+util.inspect(dogs, false, null)))
.then(function() {console.log("\n*******************************\n")})
.then(function() {console.log(util.inspect(Dog.schema.tree, false, null))})
.then(function() {console.log("\n------------------------------\n")})
.then(function() {console.log(util.inspect(Dog.schema.path("familyDogs").schema.tree, false, null))})
.catch(error => console.log(error));
});