简体   繁体   中英

sort() and limit() not working in mongoose 5.0.11

I migrated my api code to a new server couple of days back and installed all the latest packages for mongoose, express, etc. Previously, I ran the same query with mongoose version 4.12.3 and everything worked fine. But, now when I run the same query with all the upgraded packages sort() and limit() don't work.

This is the code for my api controller

'use strict';

var mongoose = require('mongoose'),
  Quad = mongoose.model('Quad');

exports.list_machine_quadData = function(req, res) {
  Quad.find({"machineId":req.body.machineId}, function(err, quad) {
    if (err)
      res.send(err);
    res.json(quad);
  }).sort({"data.eTimeStamp":-1}).limit(96); 
};

and this is the schema for the same

'use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const QuadSchema = new Schema({
    eDate:{
        type: Date,
        required: [true,'eDate is required']
    },
    eTimeStamp:{
        type: Date,
        required: [true,'eTime is required']
    },
    dataJ:{
        type: String,
        required: [true,'DataJ is required']
    }
});

const DataSchema = new Schema({
    clientId:{
        type: String,
        required: [true,'Client ID is required']
    },
    machineId:{
        type: String,
        required: [true,'Machine ID is required']
    },
    data:[QuadSchema]
  });

const Quad = mongoose.model('Quad',DataSchema);
module.exports = Quad;

were there any changes made to the sort() query and limit() in 5.X. If so, what do I need to change to make my code work? Thank you :)

You need to change your mongoose query

Replace this code

Quad.find({"machineId":req.body.machineId}).sort({"data.eTimeStamp":-1}).limit(96).exec(function(err, quad) {
    if (err)
      res.send(err);
    res.json(quad);
  }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM