用mongoose查询 - 节点JS

时间:2015-08-10 13:08:42

标签: node.js mongodb rest mongoose

所有架构中使用的基本字段。

var base_param = {
     creation_date: {
         type: Date,
         default: Date.now
     },
     modification_date: {
         type: Date,
         default: Date.now
     },
     visibility: {
         type: Boolean,
         default: true
     }
 };

 module.exports = base_param;

架构示例

var mongoose = require('mongoose');
var constant = require('../util/constant');
var Schema = mongoose.Schema;
var base_param = require('./base/base-parameter');

var time = {
    timefrom: Date,
    timeto: Date,
    holidaytype: Number,
    status: {
        type: Number,
        enum: [constant().status.pending, constant().status.accepted, constant().status.rejected, constant().status.todelete],
        default: constant().status.pending
    }
}

var holidaySchema = new Schema({
    employee: {
        type: Schema.Types.ObjectId,
        ref: 'Employee'
    },
    workday: Date,
    times: [time],
    employee_notified: {
        type: Boolean,
        default: false
    },
    hr_notified: {
        type: Boolean,
        default: false
    },
    base_param: base_param
});


module.exports = holidaySchema;

false时visibility字段表示文档已被删除(软删除)。

我想要做的是使用以下middleware在我的RESTful Web服务中使用base_param.visibility = true过滤我的查询(例如,在假日服务的查询参数中注入)。

var express = require('express');
    var routes = function () {
        var router = express.Router();
        router.use('/', function (req, res, next) {
            var query = req.query;
            query.base_param = new Object();
            query.base_param.visibility = true;
            console.log(JSON.stringify(query));
            next();
        });
        return router;
    };
    module.exports = routes;

假日控制器服务

 var controller = function (Holiday) {
        var get = function (req, res) {
            var query = req.query;
            console.log('inside holidayController\'s get, query: ' + JSON.stringify(query))
            console.log('visibility: ' + query.base_param.visibility);
            var perPage = req.query.perPage;
            var page = req.query.page;
            var date = req.query.date;
            var year = req.query.year;
            var month = req.query.month;
            if (page > 0) {
                page = page - 1;
            }
            delete req.query.perPage;
            delete req.query.page;
            delete req.query.date;
            delete req.query.year;
            delete req.query.month;

           /* query.times = {
                status: 1
            };*/

            if (date) {
                var holidayDate = new Date(date);
                holidayDate = new Date(holidayDate.getFullYear(), holidayDate.getMonth(), holidayDate.getDate());
                holidayDate.setDate(1);
                var nextMonthDate = new Date(holidayDate.getFullYear(), holidayDate.getMonth(), holidayDate.getDate());
                nextMonthDate.setMonth(nextMonthDate.getMonth() + 1);
                query.workday = {
                    "$gte": holidayDate,
                    "$lt": nextMonthDate
                };
            } else if (year && month) {
                dateRange = utility().getDateRangeByMonth(month, year);
                utility().filterQueryByWorkDay(query, dateRange);
            }
            Holiday.find(query, function (err, models) {
                    if (err) {
                        res.status(500).send(err);
                    } else {
                        utility().sortInnerArray(models, 'times', 'timefrom');
                        res.json(models);
                    }
                })
                .sort({
                    workday: 1
                })
                .limit(perPage)
                .skip(perPage * page);
        };

该服务返回一个空数组而不是数据。如果我使用Holiday.find('base_param.visibility : true', function (err, models) {,我会得到所需的列表。我不知道我错在哪里,因为我访问前面代码中的可见性字段。请帮忙。

0 个答案:

没有答案