To make MongoDB database manipulation easy, we can use the Mongoose NPM package to make working with MongoDB databases easier.
In this article, we’ll look at how to use Mongoose to manipulate our MongoDB database.
String Schema Types
We can set various properties for strings schema types.
They include:
lowercase
: boolean, whether to always call.toLowerCase()
on the valueuppercase
: boolean, whether to always call.toUpperCase()
on the valuetrim
: boolean, whether to always call.trim()
on the valuematch
: RegExp, creates a validator that checks if the value matches the given regular expressionenum
: Array, creates a validator that checks if the value is in the given array.minlength
: Number, creates a validator that checks if the value length is not less than the given numbermaxlength
: Number, creates a validator that checks if the value length is not greater than the given number
For example, we can write:
const mongoose = require('mongoose');
const connection = "mongodb://localhost:27017/test";
mongoose.connect(connection, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', () => console.error('connection error:'));
db.once('open', () => {
console.log('connected')
});
const schema = new mongoose.Schema({
test: {
type: String,
enum: ['apple', 'orange']
}
});
to make the test
field an enum.
Number Schema Types
For number schema types, we can set the following properties for the field:
min
: Number, creates a validator that checks if the value is greater than or equal to the given minimum.max
: Number, creates a validator that checks if the value is less than or equal to the given maximum.enum
: Array, creates a validator that checks if the value is strictly equal to one of the values in the given array.
Date Schema Types
For date schema types, we can set:
min
: Datemax
: Date
We can use the schema types by writing:
const mongoose = require('mongoose');
const connection = "mongodb://localhost:27017/test";
mongoose.connect(connection, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', () => console.error('connection error:'));
db.once('open', () => {
console.log('connected')
});
const personSchema = new mongoose.Schema({
name: String
});
const Person = mongoose.model('Person', personSchema);
const person = new Person({ name: { toString: () => 42 } });
person.save();
console.log(person.name);
We have the toString
method that converts 42 into a string.
The field can also be rewritten as:
const mongoose = require('mongoose');
const connection = "mongodb://localhost:27017/test";
mongoose.connect(connection, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', () => console.error('connection error:'));
db.once('open', () => {
console.log('connected')
});
const personSchema = new mongoose.Schema({
name: 'String'
});
const Person = mongoose.model('Person', personSchema);
const person = new Person({ name: { toString: () => 42 } });
person.save();
console.log(person.name);
The value is 'String'
instead of the String
constructor.
They do the same thing.
Dates
If we have date fields, we can call various methods to change its value:
const mongoose = require('mongoose');
const connection = "mongodb://localhost:27017/test";
mongoose.connect(connection, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', () => console.error('connection error:'));
db.once('open', () => {
console.log('connected')
});
async function run() {
const Assignment = mongoose.model('Assignment', { dueDate: Date });
const assignment = new Assignment({ dueDate: new Date() });
await assignment.save();
Assignment.findOne((err, doc) => {
doc.dueDate.setMonth(3);
doc.save();
doc.markModified('dueDate');
doc.save();
})
}
run();
We call the setMonth
method to set the month.
Conclusion
We can set the schema types with various properties when we create the schema with Mongoose.