如何将json对象的数组保存到mongoose?

我试图保存对象的mongoose模式的数组,但它是无法加载数据到laptop架构,我只看到一个插入包含id 。 我怎样才能将json对象的数组保存到mongoose模式?

controller.js

 var laptopData = [ { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'HP'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'HP'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'} ] var laptop = new Laptop({laptopData}); laptop.save(function(err) { // we've saved the dog into the db here if (err) throw err; }); 

schema.js

 'use strict'; var mongoose = require('bluebird').promisifyAll(require('mongoose')); var LaptopSchema = new mongoose.Schema({ name: String, ram:String, harddrive:String, Brand: String }); module.exports = mongoose.model('Laptop', LaptopSchema); 

Solutions Collecting From Web of "如何将json对象的数组保存到mongoose?"

您应该单独创build对象并保存它们。 你不能像这样传递一个数组。 用一些东西来代替

 var laptopData = [...]; for(var laptopItem in laptopData){ new Laptop(laptopData[laptopItem]) .save() .catch((err)=>{ console.log(err.message); }); }