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.
Models
We can create models so we can use them as templates to create a document in the MongoDB database.
For example, we can write:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test')
const schema = new mongoose.Schema({ name: 'string', size: 'string' });
const Tank = mongoose.model('Tank', schema);
const small = new Tank({ size: 'small' });
small.save((err) => {
if (err) {
return console.log(err);
}
});
We called mongoose.Schema
that has the name
and size
string fields.
Then we create the model with the schema with the mongoose.model
method.
Next, we use the model class to create the document with the data we want.
Then we call save
to save the document.
The tank
collection will be created if it’s not already created.
Then the document will be created.
The callback we pass into the save
method has the err
parameter that will be defined if there’s an error.
We can also add the create
static method to create the document:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test')
const schema = new mongoose.Schema({ name: 'string', size: 'string' });
const Tank = mongoose.model('Tank', schema);
Tank.create({ size: 'small' }, (err, small) => {
if (err) {
return console.log(err);
}
console.log(small);
});
The first argument is the document we want to create.
And the 2nd argument is the callback that’s called when the result is computed.
Also, we can call the insertMany
static method on the Tank
model:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test')
const schema = new mongoose.Schema({ name: 'string', size: 'string' });
const Tank = mongoose.model('Tank', schema);
Tank.insertMany([{ size: 'small' }], (err) => {
if (err) {
console.log(err);
}
});
If we create a custom collection, then we can use the model
function to create the model:
const mongoose = require('mongoose');
const connection = mongoose.createConnection('mongodb://localhost:27017/test');
const schema = new mongoose.Schema({ name: 'string', size: 'string' });
const Tank = connection.model('Tank', schema);
Tank.insertMany([{ size: 'small' }], (err) => {
if (err) {
console.log(err);
}
});
Querying
We can query documents with the find
method.
For example, we can write:
const mongoose = require('mongoose');
const connection = mongoose.createConnection('mongodb://localhost:27017/test');
const schema = new mongoose.Schema({ name: 'string', size: 'string' });
const Tank = connection.model('Tank', schema);
Tank.find({ size: 'small' }).where('createdDate').gt(365).exec((err, tanks) => {
if (err) {
return console.log(err)
}
console.log(tanks);
});
We call find
with the key and value we’re looking for.
The where
method has the field that we want to search for.
gt
searches for something that’s greater than.
exec
runs the query, and the callback has the results.
Deleting
We can delete items with the deleteOne
method:
const mongoose = require('mongoose');
const connection = mongoose.createConnection('mongodb://localhost:27017/test');
const schema = new mongoose.Schema({ name: 'string', size: 'string' });
const Tank = connection.model('Tank', schema);
Tank.deleteOne({ size: 'large' }, (err) => {
if (err) {
return console.log(err);
}
});
We delete the first item that has the size
field equal to 'large'
.
Conclusion
We can manipulate MongoDB documents via models in with Mongoose.