簡體   English   中英

使用Node.js將對象數組插入mongodb

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

現在,我正在使用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);

情緒應該是這樣的( 檢查這張圖片 )。

但是mongo保存了一個空數組( 請檢查此圖像 )。

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