使用mongoose

时间:2015-11-08 13:04:21

标签: node.js mongodb mongoose

获得以下帮助:

1。https://stackoverflow.com/a/30164855/2168120

2。mongodb auto-increment

app.js:

//some codes
var mongoose = require('mongoose');
var connection = mongoose.createConnection("mongodb://localhost/dfp", function(err){
    if(err){
      console.log(err);
    }else{
      console.log("connected to mongodb from app");
    }
});

模式:

var userSchema = mongoose.Schema({email: String, fname: String, lname: String, password: String, gender: String,
                                mobile: String, dob:{ type: Date, default: Date.now }, ip: String, doj:{ type: Date, default: Date.now }
                                },{ collection : 'user' }); 
var user = mongoose.model('user',userSchema);

var CounterSchema = mongoose.Schema({
  _id: {type: String, required: true},
  seq: { type: Number, default: 0 }
});

var counters = mongoose.model('counters', CounterSchema);

app.post():

app.post('/save', function(req, res, next){
  var data = req.body;
  var newuser = new user();
  var dob = new Date(data['dob']);
  var doj = new Date();
  for(var i in data){
    if(i != dob){
      newuser.i = data[i];
    }       
  }
  newuser.dob = dob;

  counters.findByIdAndUpdate({_id: 'userid'}, {$inc: { seq: 1} }, function(error, counter)   {
    if(error) {
        return next(error); //https://stackoverflow.com/a/30164855/2168120
    }
    newuser._id = counter.seq;
    next();
  });
  console.log(newuser.get('_id'));

  newuser.save(function (err) {
    if (err) {
      return handleError(err);
    }
    // saved!
  });
});

我无法将此新记录保存到用户表中。

1 个答案:

答案 0 :(得分:1)

感谢this

  1. 使用mongoose-auto-increment我可以创建一个自动递增的_id。
  2. 解决方案:

    var mongoose = require('mongoose');
    var autoIncrement = require('mongoose-auto-increment'); 
    var connection = mongoose.createConnection("mongodb://localhost/dfp", function(err){
       if(err){
         console.log(err);
       }else{
         console.log("connected to mongodb from app");
       }
    });
    autoIncrement.initialize(connection);
    ///////
    var userSchema = mongoose.Schema({email: String, fname: String, lname: String, pwd: String, gender: String,
                                    mobile: String, dob: { type: Date, default: Date.now }, ip : String, doj: { type: Date, default: Date.now }
                                    },{ collection : 'user' }); 
    userSchema.plugin(autoIncrement.plugin, 'user');
    var user = connection.model('user',userSchema);
    ////////
    
    1. 问题出在 for 循环中;设置字段。
    2. 解决方案:

      app.post('/save', function(req, res, next){
        var data = req.body;
        var dob = new Date(data['dob']).toISOString();
        var doj = new Date().toISOString();
      
        var newuser = new user();
        for(var i in data){
           if(i != dob) { newuser.set(i, data[i]); }
        }
        newuser.dob = dob;    newuser.doj = doj;
      
        newuser.save(function(error, users){
          if (error) {  next();  }
          // Return ID of element in response.
         console.log(users._id);
        });
      
      });