简体   繁体   English

使用Node.js将对象数组插入mongodb

[英]Insert an array of objects into mongodb with nodejs

Now I am using clmtrackr library to detect emotions from the webcam and I want to save this emotions Here is my node.js code to save the values in 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"});
}

and the schema code is 架构代码是

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);

emotions should be like that ( check this image ). 情绪应该是这样的( 检查这张图片 )。

but the mongo saved an empty array ( check this image ). 但是mongo保存了一个空数组( 请检查此图像 )。

The emotions parameters of your storeemotion function is already an array, so you just have to pass that parameters as is, not in another array: 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"});
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM