简体   繁体   中英

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

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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