Mongodb primary series 3: Mongoose



Part oneMongodb primary series 2: adding, deleting, modifying and querying in nodeThis article will continue to explain how to use mongoose to control mongodb more smoothly.

What is mongoose

Mongoose is a node module. It provides a more efficient scheme to operate mongodb in node. It provides an interface to mongodb collection and some practical functions, such as schema hierarchy, middleware and data verification.

Schema definition

First, you need to install mongoose. The command to install mongoose is as follows:npm install mongooseAt the same time, remember to start the mongodb service.

In Mongoose, everything is driven by schema. In the following program, we define a task. You can understand that this task corresponds to a set of tasks in mongodb. The collection has two properties, one is project, the type is string, the other is description, the type is string.

var mongoose = require('mongoose')

var db = mongoose.connection;
db.on('error', function(error) {

//Schema definition
var Tasks = new mongoose.Schema({
  Project: {type: string, default: 'default task'},
  description: String
mongoose.model('Task', Tasks);


// add
var Task = mongoose.model('Task');
var task = new Task();
task.description = 'task1 description.'; {
  if (err) throw err;
  console.log('Task saved.')

If there is no task set in the database demodb, the task set will be created automatically when the above program is executed. After the program is executed, we find that there is one more line of data in the task set in the database
Mongodb primary series 3: Mongoose

If we don’t take care task.description The value of is given the number 123, so can the program run normally?
Of course, mongoose will help us automatically convert the number 123 to the string 123. The end result would be this:
Mongodb primary series 3: Mongoose


You can use the internal ID of the document to obtain and delete the document. The code is as follows:

var Task = mongoose.model('Task')
Task.findById('5b56ce3c2fa17f02e459fe54', function(err, task) {

Of course, you can use other conditions to delete. The code is as follows:

var Task = mongoose.model('Task')
Task.findOne({ 'description': '12' }, function(err, task) {


The following code updates a document ID 5b5bbbfbf04e70bd432bde1 with mongoose

var Task = mongoose.model('Task');
  {_id: '5b5bebbfbf04e70bd432bde1'},
  {description: 'update description.'},
  {multi: false},
  function(err, rows_updated) {
    if (err) throw err;

Of course, you can also use the find method of mongoose to locate a document, then modify and save it.


The following code searches the tasks collection, outputs the unique ID and description of each task, and closes the database connection after printing.

var Task = mongoose.model('Task');
Task.find((err, tasks) => {
  if (tasks != null) { => {
      console.log('ID:' + v._id);

Of course, there are many query methods, here is just one of them.

Visualization tool recommendation

By the way, you can use the compass tool released by mongodb to view and operate data (including adding, deleting, modifying and querying), which is very convenient for testing and debugging data.


Do you think you are a master of mongodb? Maybe not, but after reading the initial trilogy of this series, I believe you should have mastered a lot of basic knowledge. If you decide to use mongoose in depth, please refer to its online documentation…

So far, the preliminary series of mongodb will come to an end. At the same time, the following books are recommended for those who need to understand mongodb in depth:
Mongodb authoritative guide (2nd Edition)
Mongodb (2nd Edition)

Recommended Today

Background management system menu management module

1 menu management page design 1.1 business design Menu management, also known as resource management, is the external manifestation of system resources. This module is mainly to add, modify, query and delete the menu. CREATE TABLE `sys_menus` ( `id` int(11) NOT NULL AUTO_INCREMENT, `Name ` varchar (50) default null comment ‘resource name’, `URL ` varchar […]