mongooseDate.js
First install the mongoose module by doing
npm i mongoose
in the project directory. Let us check it by example:
"use strict";
/*
* include more sophisticated mongodb functionality
* mongoose enforces schemas, mongodb doesn't
*/
const mongoose = require("mongoose");
/*
* create schema for database object
* build corresponding model as an object
* Wex19, lesson 14
*/
const carSchema = mongoose.Schema ({
carType: String,
notes: String,
range: Number,
created: {type: Date, default: Date.now}
});
const Car = mongoose.model("Car", carSchema, 'car');
/*
* connect to mongodb server
*/
const dbname = "test111";
const constr = `mongodb://localhost:27017/${dbname}`;
const conparam = { useNewUrlParser: true, useUnifiedTopology: true };
mongoose.connect(constr, conparam);
const db = mongoose.connection;
db.once("open", function() {
console.log("Connected to server by mongoose")
});
/*
* create concrete object
*/
var car1 = new Car({
carType: "SUV",
notes: "my Suzuki",
range: Math.random() * 600
});
/*
* Utility function to reformat mongdb dates which are in ISO8601 fmt
* totally unrelated to mongoose :)
*/
const dateConverter = function(dato) {
let dstr = "";
if (dato.getDate() < 10) {
dstr += "0" + dato.getDate();
} else {
dstr += dato.getDate();
}
dstr += "/";
if (dato.getMonth() < 9) {
dstr += "0" + (dato.getMonth() + 1);
} else {
dstr += (dato.getDate() + 1);
}
dstr += "/" + dato.getFullYear();
return dstr;
}
/*
* insert car1 object into database, the C of CRUD
* save is a mongoose method
*/
car1.save(function(error, savedDocument) {
if (error) console.log(error);
console.log(savedDocument);
var dato = savedDocument.created;
// checking the date utility
var dato1 = dateConverter(dato);
console.log(dato1);
});
/*
* alternative way of inserting, the C of CRUD
* create below includes the save functionality
*/
Car.create(
{
carType: "Sedan",
notes: "boring",
range: Math.random() * 600
},
function(error, savedDocument) {
if (error) console.log(error);
console.log(savedDocument);
db.close(); // if forgotten batch job doesn't stop by itself
}
);
console.log("Asynchronous? If I come first, yes!");
mongooseSort.js
Again first install the mongoose module by doing
npm i mongoose
in the project directory. This example has the useful extra benefit that it exemplifies.
"use strict";
// https://thecodebarbarian.com/how-find-works-in-mongoose
/*
* include more sophisticated mongodb functionality
* mongoose enforces schemas, mongodb doesn't
*/
const mongoose = require("mongoose");
const constr = "mongodb://localhost:27017";
const conparam = { useNewUrlParser: true, useUnifiedTopology: true };
const tester = async function (dbname) {
/*
* create schema for database object
* build corresponding model as an object
* Wex19, lesson 14
*/
const carSchema = mongoose.Schema ({
carType: String,
notes: String,
range: Number,
created: {type: Date, default: Date.now}
});
const Car = mongoose.model("Car", carSchema, 'car');
/*
* connect to mongodb server
*/
await mongoose.connect(constr + "/" + dbname, conparam);
const db = mongoose.connection;
db.once("open", function() {
console.log("Connected to server by mongoose")
});
const arr = await Car.find({}, null, {sort: {range: 1, created: -1}});
console.log(arr)
db.close();
}
console.log("Asynchronous? If I come first, yes!");
tester("test111").catch(error => console.log(error.stack));