Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

666
Views
How to save array of json object to mongoose?

I am trying to save array of object to mongoose schema but it is failing to load data into laptop schema i only see one insertion that contains id. How can i save array of json object to mongoose schema ?

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);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You should individually create objects and save them. You can't pass an array like that. Use something in the lines of

var laptopData = [...];
for(var laptopItem in laptopData){
    new Laptop(laptopData[laptopItem])
      .save()
      .catch((err)=>{
        console.log(err.message);
      });
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!