我有这样的问题。我正在用Express制作API。在其中,我使用猫鼬创建了MongoDB连接。在此API中,我创建了一个按日期对元素进行排序的函数。这是我的功能,
module.exports.findAll = function (callback) {
const query = {state: "Confirmed"};
Reservation.find(query, {sort: '-date'} , callback);
}
当我使用POSTMan进行检查时,它仅返回特定元素的ID。
这是我的控制器文件。
const express = require('express');
var router = express.Router();
var Reservation = require('../models/reservation');
router.get("/", function (req, res) {
Reservation.findAll(function (err, reservations) {
if(err) throw err;
if (!reservations){
res.json({state:false,msg:"No reservations found"});
}
if(reservations){
res.json(reservations);
}
})
});
router.post("/create",function (req,res) {
const newReservation = new Reservation({
date: req.body.date,
from: req.body.from,
to: req.body.to,
lab: req.body.lab,
name: req.body.name,
email: req.body.email,
role: req.body.role,
year: req.body.year,
reason: req.body.reason,
state: "Not Comfirm"
});
Reservation.saveReservation(newReservation, function (err, reservation) {
if (reservation) {
res.send(reservation);
}
else {
console.log('Error in User save :' + JSON.stringify(err, undefined, 2));
}
});
});
router.get('/notComfirmed', function (req, res) {
Reservation.findNotComfirmed(function (err, reservations) {
if(err) throw err;
if (!reservations){
res.json({state:false,msg:"No reservations found"});
}
if(reservations){
res.json(reservations);
}
})
});
router.post('/confirm', function(req, res){
const _id= req.body._id;
Reservation.updateReservation(_id, function (err, reservation) {
if(err) throw err;
if (!reservation){
res.json({state:false,msg:"Something went wrong"});
}
if(reservation){
res.json(reservation);
}
})
});
router.post('/remove', function(req, res){
const _id= req.body._id;
Reservation.remove(_id, function (err, reservation) {
if(err) throw err;
if (!reservation){
res.json({state:false,msg:"Something went wrong"});
}
if(reservation){
res.json(reservation);
}
})
});
module.exports = router;
这是我的模型的样子,
const mongoose = require('mongoose');
var DateOnly = require('dateonly');
const schema = mongoose.Schema;
const reservationSchema = new schema({
date:{type:String, required:true},
from:{type:String, reason:true},
to:{type:String, required:true},
lab:{type:String, required:true},
name:{type:String, required:true},
email:{type:String, required:true},
role:{type:String, required:true},
year:{type:String},
reason:{type:String, required:true},
state:{type:String, required:true},
});
const Reservation = module.exports= mongoose.model("Reservation",reservationSchema );
module.exports.saveReservation = function (newReservation, callback) {
newReservation.date=new DateOnly(newReservation.date);
newReservation.save(callback);
}
module.exports.findBYDateAndTime = function(date, time, lab, callback){
const query = {date:date,time:time, lab:lab};
Reservation.findOne(query,callback);
}
module.exports.findNotComfirmed= function (callback) {
const query = {state: "Not Comfirm"};
Reservation.find(query, callback);
}
module.exports.updateReservation = function (id, callback) {
var reservation ={
state: "Confirmed"
}
Reservation.findByIdAndUpdate(id, { $set: reservation}, {new: true}, callback) ;
}
module.exports.remove = function (_id, callback) {
Reservation.findByIdAndRemove(_id, callback);
}
module.exports.findAll = function (callback) {
const query = {state: "Confirmed"};
Reservation.find(query, {sort: '-date'} , callback);
}
有人可以帮我获取按日期排序的已确认预订的所有详细信息吗?我用了很多方法,但是它总是只返回那些保留的ID。
谢谢!
答案 0 :(得分:1)
在猫鼬查询中,第二个参数保留给我们要从mongodb中选择的字段。
我在stackoverflow How to sort in mongoose?
上找到了一些答案尝试这个
Reservation.find({}, null, {sort: {date: -1}}, function(err, docs) { ... });