mongodb中另一个集合的增量字段

时间:2016-09-29 06:54:46

标签: javascript node.js mongodb

我创建了两个集合,一个用于企业,另一个用于员工,其架构如下,



var mongoose= require('mongoose');

var Enterprise= new mongoose.Schema({
	
	name:{type:String},
	email:{type:String},
	sector:{type:String},
	employees: {type:Number,default:0}

});


module.exports={
	Enterprise:Enterprise
};






var mongoose = require('mongoose');

var employee = new mongoose.Schema({
    
    enterprise:{type: String},
	name:{type:String},
	email:{type:String},
	password:{type:String},
	gender:{type:String},
});

module.exports = {

	employee:employee
};




我的添加员工路线



var mongoose = require('mongoose');
var q = require('q');

var employee = mongoose.model('employee');
var enterprise = mongoose.model('enterprise');

var addEmployee = function(req, res) {

    newEmployee = new employee();

    newEmployee.enterprise = req.params.enterprise;
    newEmployee.name = req.params.name;
    newEmployee.email = req.params.email;
    newEmployee.gender = req.params.gender;

    function detailSave() {

        var deferred = q.defer();

        newEmployee.save(function(err, data) {

            if (err) {
                res.send(500);
                console.log('couldnt save employee details');
                deferred.reject({errmessage: 'couldnt save employee details', err: err});
            } else {

                res.send(200);
                deferred.resolve({data: data});
            }
        });

        return deferred.promise;
    }

    function incrementEmployee(doc) {

        var deferred = q.defer();

        enterprise.findOneAndUpdate({ 'name': doc.enterprise }, { $inc: { 'employees': 1 } },

            function(err, num) {
                if (err) {
                    deferred.reject({errmessage: 'couldnt incrementEmployee', err: err});
                    res.send(500);
                    console.log('couldnt incrementEmployee');
                } else {
                    res.send(200);
                    deferred.resolve({num:num});
                }
            });

        return deferred.promise;

    }

    detailSave()
        .then(incrementEmployee)
        .then(function(success) {
            console.log('success', success);
            res.json(200, success);
        })
        .fail(function(err) {
            res.json(500, err);
        })
        .done();

};
module.exports = {
    addEmployee: addEmployee
};




问题是当我添加员工时,企业集合中的员工字段不会增加

1 个答案:

答案 0 :(得分:0)

我认为您的查询无效,因为doc.enterprisenull 根据你的评论。

尝试像query这样{'name': doc.data.enterprise}

function incrementEmployee(doc) {

  var deferred = q.defer();

  enterprise.findOneAndUpdate({
      'name': doc.data.enterprise
    }, {
      $inc: {
        'employees': 1
      }
    },

    function(err, num) {
      if (err) {
        deferred.reject({
          errmessage: 'couldnt incrementEmployee',
          err: err
        });
        res.send(500);
        console.log('couldnt incrementEmployee');
      } else {
        res.send(200);
        deferred.resolve({
          num: num
        });
      }
    });

  return deferred.promise;

}