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.
Buffer
We can declare the Buffer
type with the Buffer
constructor:
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 Binary = mongoose.model('binary', { binData: Buffer });
Mixed
We can also add a mixed type to let us add anything as the value for a field:
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 Mixed = mongoose.model('mixed', { any: mongoose.Mixed });
We can’t autodetect and save changes when we use this type since it’s a schema-less type.
ObjectIds
We can specify the ObjectId type to store object IDs.
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 carSchema = new mongoose.Schema({ driver: mongoose.ObjectId });
const Car = mongoose.model('Car', carSchema);
const car = new Car();
car.driver = new mongoose.Types.ObjectId();
console.log(typeof car.driver)
console.log(car.driver instanceof mongoose.Types.ObjectId);
car.driver.toString();
We created a carSchema
to store data about cars.
Inside the schema, we have the driver
field, which is of type mongoose.ObjectId
.
We can create the object ID to assign with the mongoose.Types.ObjectId()
method.
Then we can check the types of car.driver
. We should get the type is 'object'
.
The 2nd console log should be true
since driver
is of type ObjectId
.
Boolean
Another type that we set to fields is the boolean type.
The following values are cast to true
:
true
'true'
1
'1'
'yes'
And these values are cast to false
:
false
'false'
0
'0'
'no'
For example, if we 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 M = mongoose.model('Test', new mongoose.Schema({ b: Boolean }));
console.log(new M({ b: 'nay' }).b);
console.log(mongoose.Schema.Types.Boolean.convertToFalse);
mongoose.Schema.Types.Boolean.convertToFalse.add('nay');
console.log(new M({ b: 'nay' }).b);
We called mongoose.Schema.Types.Boolean.convertToFalse
to let us cast 'nay'
to false
.
Arrays
Mongoose has an array type that lets us specify arrays.
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 ToySchema = new mongoose.Schema({ name: String });
const ToyBoxSchema = new mongoose.Schema({
toys: [ToySchema],
strings: [String],
});
We have the ToySchema
Mongoose schema, which we specify as the type of the toys
array field.
Each entry of the toys array must conform to the ToySchema
.
Conclusion
We can specify various types with Mongoose to build our schema.