Using mongoose operation to add, delete, modify and query mongodb

Using mongoose to operate mongodb's test file

  1. Connect to database
    1.1 introduction of mongoose
    1.2 connect to the specified database (only the database changes in URL)
    1.3 get connection object
    1.4 monitoring of binding connection completion (used to prompt connection success)
  2. Get the Model corresponding to a specific set
    2.1 literal Schema (describing document structure)
    2.2 define the Model (corresponding to the set, which can be operated)
  3. CRUD the collection data through the Model or its example
    3.1 add data through save() of Model instance
    3.2 query multiple or one data through find()/findOne() of Model
    3.3 update a data through findByIdAndUpdate() of the Model
    3.4 delete matching data through deleteOne() of Model

  • */

Download mongoose dependency package

npm install --save mongoose 

Download md5 encryption dependency package

npm install --save blueimp-md5

The following code location: db_test.js

// Introduce md5 dependency
const md5 = require('blueimp-md5')

1. Connect to database

// 1.1 introduction of mongoose
const mongoose = require('mongoose')
// 1.2 connect to the specified database (only the database changes in URL)
mongoose.connect('mongodb://localhost:27017/zp_test', {useNewUrlParser: true})
// 1.3 get connection object
const conn = mongoose.connection
// 1.4 monitoring of binding connection completion (used to prompt connection success)
conn.on('connected', function () {
  console.log('Database connection succeeded!')
})

2. Get the Model corresponding to a specific set

// 2.1 literal Schema (describing document structure)
const userSchema = mongoose.Schema({
  username: {type: String, require: true}, // User name
  password: {type: String, required: true}, // Password
  type: {type: String, required: true} // User type: job seeker / boss
})
// 2.2 define the Model (corresponding to the set, which can be operated)
const UserModel = mongoose.model('users', userSchema) // Collection name: users

3. CRUD the set data through the Model or its example

 3.1 adopt Model Instance save() Add data
function testSave() {
  // user data object
  const user = {
    username: 'mandy',
    password: md5('1234'),
    type: 'Job seeker'
  }
  // Create Model instance
  const userModel = new UserModel(user)
  // Or add data like this
  // const userModel = new UserModel({username: 'Tom', password: md5('3333'), type:' boss'})
  
  // Save to database
  userModel.save(function (err, user) {
    console.log('save', err, user)
  })
}
// testSave()

 3.2 adopt Model Of find()/findOne() Query multiple or one data
function testFind() {
  // Find multiple
  UserModel.find(function (err, users) { // If there is a match, return a [user, user...], if there is no match, return []
    console.log('find()', err, users)
  })
  // Find one
  UserModel.findOne({_id: '5cdf99ebf3539334948ae2c8'}, function (err, user) { // If a match returns a user
    console.log('findOne()', err, user)
  })
}
// testFind()

 3.3 adopt Model Of findByIdAndUpdate() Update a data
function testUpdate() {
  UserModel.findByIdAndUpdate({_id: '5cdfacc55684652a08b49014'}, {username: 'A boss'}, function (err, user) {
    console.log('findByIdAndUpdate()', err, user)
  })
}
// testUpdate()

 3.4 adopt Model Of remove() Delete matching data
function testDelete() {
  UserModel.deleteOne({_id: '5cdf9d761a1c050c70bc0d09'}, function (err, result) {
    console.log('deleteOne()', err, result)
  })
}
// testDelete()

Function:

node db_test.js

You can view the effect in mongodb's visualizer

Visualization tools

Mongo Plugin in webstorm

Robo 3T

Download address: https://robomongo.org/download

Tags: node.js Mongoose Database MongoDB npm

Posted on Fri, 08 Nov 2019 12:16:15 -0500 by waldo