Mongoose save不会插入任何东西

时间:2016-01-06 15:50:28

标签: mysql node.js mongodb mongoose

var mongoose = require('mongoose');
var url = mongoUrl + dbCollectionName; //external var
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
    console.log(getTimeStamp() + 'Connected to Mongo with Mongoose');
    insertDocuments(db, function () {
    });
});

mongoose.connect(url);

// Creating our Schema
var ackSchema = mongoose.Schema({
hostgroup : String,
host : String,
service : String,
timePeriod : String,
startTime : Number,
ackTime : Number,
deltaTime : Number,
caseNumb : String,
state : Number,
author : String,
ackId : Number,
});

// Creation our model
var ackModel = mongoose.model(collectionName, ackSchema);

//////////////////////////////////////
//// Functions

var insertDocuments = function(db, callback) {
    // Get the documents collection
    var collection = db.collection(collectionName);
    var returnValue = 0,
        caseValue = "";

    // Find item to compare
    collection.find({}).toArray( function (err, docs) {
        // If Collection does not exist => 0, else 1
        if (docs.length === 0) {
            colExist = 0;
            console.log(getTimeStamp() + 'Collection is empty. If needed application will create "' + collectionName + '" collection while inserting items.');
        } else {
            colExist = 1;
            console.log(getTimeStamp() + 'Collection "' + collectionName + '" exist, item will be added');
        };

        // SQL Query to fetch row
        mysqlQuery(function (row) {
        //// IF 8/20
            if (row.timeperiod == timePeriod820) {
                returnValue = timePeriodCalc(row); // Function from timeperiod.js
                deltaValue = parseInt(returnValue.slice(6)); // Get our Delta Value
                caseValue = returnValue.slice(0,4); // Get the Case Number, verification purpose
        //// ELSE 24/7
            } else {
                deltaValue = (row.ack_time - row.start_time);
                caseValue = 'C.0';
            };

            var ack = new ackModel({
                hostgroup : row.hostgroup,
                host : row.hostname,
                service : row.servicename,
                timePeriod : row.timeperiod,
                startTime : row.start_time,
                ackTime : row.ack_time,
                state : row.state,
                deltaTime : deltaValue,
                caseNumb : caseValue,
                author : row.author,
                ackId : row.ack_id
             });

            console.log(ack);

            ack.save(function (err, ack) {
                if (err) {
                    console.log(getTimeStamp() + 'Mongoose .save error : ' + err);
                }
                else { 
                    console.log(getTimeStamp() + 'SAVE Mongoose : ' + ack.author + ' ' + ack.ackId); 
                }
            });
        }); 
    callback(docs);
    });
};

正如您所看到的,我从MySQL数据库中获取一些原始数据以将它们放入mongo数据库中。问题是当ack var完全准备好保存时,ack.save()不会保存任何东西。

这是ack的填充方式(来自console.log(ack)):

{ _id: 568d360867e31d11001206f7,
 ackId: 1087,
 author: 'pilote1',
 caseNumb: 'C.10',
 deltaTime: -3481,
 state: 2,
 ackTime: 1452091450,
 startTime: 1452094931,
 timePeriod: 'RESSOURCES-GPE-N1-8/20',
 service: 'Service-Random-1M',
 host: 'PRO_HOST3',
 hostgroup: 'PROSERVIA' }

0 个答案:

没有答案