mongodb database and data paging

Posted by Torleif on Sun, 19 May 2019 05:58:20 +0200

When working on a small project of your own, you learned about mongodb non-relational databases and used mongoose encapsulated query methods, including the limit and skip methods used for database paging, which are recorded here.

1. mongodb database connection

  • Referring to the official website documents, the corresponding parameters are as follows:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
  • Connecting to a database using mongoose
const dataBaseUrl = config.admin.username
  ? `mongodb://${config.admin.username}:${config.admin.pwd}@${config.host}/share-resource`
  : `mongodb://${config.host}/share-resource`;
mongoose.connect(dataBaseUrl, { useNewUrlParser: true });
  • If a warning message appears that a new compilation method is required, add useNewUrlParser: true when connecting

DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version.
To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

mongoose.connect(dataBaseUrl, { useNewUrlParser: true });
  • Listen for connection operations when connecting to a database
mongoose.connection.on('connected', function() {
  console.log('Mongoose connection open to ' + dataBaseUrl);
});
/* Connect database exception */
mongoose.connection.on('error', function(err) {
  console.log('Mongoose connection error:' + err);
});
/* Disconnect database */
mongoose.connection.on('disconnected', function() {
  console.log('Mongoose connection disconnected');
});

2. Data types (schemaTypes provided in mongoose)

  • Data types are String, Number, Date, Buffer, Boolean, ObjectId, Array, Mixed, Map, Decimal128
  • When inserting data directly into a database using the insert method, the default is to insert a double number unless the type of number is enforced

3. mongoose's approach to database operations

Insertion of 3.1 Data

  • New schema file first
const mongoose = require('../database/mongodbHelper');
const Message= mongoose.Schema;
const RecordModel = new Message({
  message: String,
  name: String,
  num: Number,
},{
  versionKey: false
});
module.exports = mongoose.model('using_records', RecordModel);
  • When inserting data using schema, direct insertion adds a _v field to the new set, which represents the version number of the set. You can delete the _v field by adding versionKey: false to the schema
  • Data insertion: using save method
const record= new Record({
    message: req.body.message,
    name: req.body.name,
    num: req.body.num,
});
record.save((err, docs) => {
  if (err) {
    res.send({ 'status': -1, 'msg': 'Insert failed' });
  } else {
    res.send({ 'status': 200, 'msg': 'Insert Successful', 'result':  ''});
  }
});

3.2 Query of data

  • Using the find method
record.find((err, docs) => {
  if (err) {
    res.send({ 'status': -1, 'msg': 'Parameter error' });
  } else {
    res.send({ 'status': 200, 'msg': 'query was successful', 'result':  docs});
  }
});

3.3 Update of data

  • Update a data: updateOne
/* The first parameter is the query parameter, the second is the content to be updated, and the third is the callback method */
record.updateOne({_id: id}, updateInfo, (err, doc) => {
    if(err) {
      res.send({'status': -1, 'msg': 'Update failed', 'result': ''});
    } else {
      res.send({'status': 200, 'msg': 'Update Successful', 'result': ''});
    }
})
  • Update Multiple Data: updateMany
record.updateMany({user_info: {$elemMatch: {user_id: userId, status: 1, is_delete: 1}}}, {$set: {'user_info.$.is_delete': 3}}, (err, doc) => {
    if(err) {
      res.send({'status': -1, 'msg': 'Parameter error'});
    } else {
      res.send({'status': 200, 'msg': 'Empty Success'});
    }
})

3.4 Deletion of data

/* The first parameter is the content to be deleted */
record.findOneAndDelete({_id: req.body.id}, (err, doc) => {
    if(err) {
      res.send({'status': -1, 'msg': 'Delete failed'});
    } else {
      res.send({'status': 200, 'msg': 'Delete succeeded'});
    }
})

4. Paging operations on databases (limit and skip methods)

  • limit() method is to limit the number of data bars per query; skip(param) skips param bars and does not query
/* page: Page number; pagesize: number of pages per page */
let page = req.body.page;
let pagesize = req.body.pagesize;
let queryResult = collection.find(queryCondition).limit(pageSize).skip((page - 1) * pageSize).sort({'_id': -1});
queryResult.exec((err, value) => {
  if(err) {
    reject(err);
  } else {
    resolve({total, value});
  }
})

5. Matching data

  • Match a field in an object in an array of data and set the corresponding value with $set
$set: {'user_info.$.status': 1}
  • $elemMath only matches the first data and returns only the first data when there are multiple identical data in the array
let arr = [
    {
        is_delete: 1,
        name: 'a'
    },
    {
        is_delete: 1,
        name: 'b'
    }
]
{$elemMatch: {is_delete: 1}}Match only arr First Data
  • aggregate matches multiple data
/* aggregate Aggregation, $unwind splits the array into individual elements
   * $group group by
   * $sum Statistics
   * $project Filter returned values to return a filtered field
   * */
  message.aggregate([
    {
      $match: {
        'user_info.user_id': id,
        'user_info.is_delete': 0
      }
    },
    {
      $unwind: '$user_info'
    },
    {
      $group: {
        _id: {status: '$user_info.status',},
        count: {$sum: 1}
      }
    },
    {
      $project: {
        '_id': 0,
        'status': '$_id.status',
        'count': 1
      }
    }
  ]).then()
  • For a field in a matching array
let arr = [
    {
        is_delete: 1,
        name: 'a'
    },
    {
        is_delete: 1,
        name: 'b'
    }
]
/* Matching name in arr */
$match: {
    'arr.name': 'a'
}
/* Grouping Filtering */
$ group: {
    _id: {name: '$arr.name'}
}
  • Insert data into an array in an object
let obj = {
    id: 1,
    arr: [
        {
            is_delete: 1,
            name: 'a'
        },
        {
            is_delete: 1,
            name: 'b'
        }
    ]
}
{'$push': {arr: {name: 'c', is_delete: 0}}}

You're working hard. If it helps you, leave a mark on your learning. (Give me a pat on the back.)

Topics: node.js Mongoose Database MongoDB Mobile