用nodejs将一系列对象插入MongoDB



现在,我正在使用clmtrackr库来检测网络摄像头的情绪,我想在这里保存这种情绪是我的node.js代码,以保存mongodb

中的值
 exports.storeemotion = function (emotions, callback){
  var eshema= new emotioncollection({ 
    emotions: [emotions]
  });
  eshema.save(function(err) {
          }); 
  callback({"status":"emotion remote done"});
}

和架构代码为

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;
//var bcrypt         = require('bcrypt-nodejs');
// search results schema 
var ResultaSchema   = new Schema({
 emotions:[{emotion: String, value: String}]
});

module.exports = mongoose.model('emotioncollection',ResultaSchema);

情绪应该是这样的(检查此图像(。

但是蒙哥保存了一个空数组(检查此图像(。

storeemotion函数的 emotions参数已经是一个数组,因此您只需要按原样传递该参数,而不是在另一个数组中:

exports.storeemotion = function (emotions, callback) {
  var eshema = new emotioncollection({ 
    emotions: emotions  // <= your schema already expects an array of objects
  });
  eshema.save(function(err) {
    if (err) return callback({"status": "error"});
    callback({"status": "emotion remote done"});
  });
}

最新更新