0

My object is

[ { Indicator: 'RA', Year: '2019' },
  { Indicator: 'RR', Year: '2019' },
  { Indicator: 'ROOM REV', Year: '2019' },
  { Indicator: 'F&B REV', Year: '2019' },
  { Indicator: 'TOTAL REV', Year: '2019' },
  { Indicator: 'ROOM PROFIT', Year: '2019' },
  { Indicator: 'F&B PROFIT', Year: '2019' },
  { Indicator: 'GOP', Year: '2019' },
  { Indicator: 'BASIC', Year: '2019' },
  { Indicator: 'TRAD', Year: '2019' },
  { Indicator: 'INC', Year: '2019' } ]

and I want to add that object to single document.

Nodejs code;

router.post('/api/sendJsonTable', function(req,res,next){
  var body = req.body
    console.log(body)
    Data.create(body, function (error, data) {
      if (error) {
        return next(error);
      } 
    });

})

models

var DataSchema = new mongoose.Schema({
    value: {
        type: String,
        trim: true
    },
    indicator: {
        type: String,
        trim: true
    },

})

When try that code, the code create 11 documents.

mahmut K
  • 11
  • 4
  • Use `JSON.stringify(body)` – Ramesh S Mar 29 '19 at 12:55
  • You mean an "embedded array". See also [Subdocuments](https://mongoosejs.com/docs/subdocs.html) in the mongoose documentation for other examples of defining an embedded array schema – Neil Lunn Mar 29 '19 at 13:06

1 Answers1

0

Change your schema to something like

var DataSchema = new mongoose.Schema({
    something : {
      value: {
        type: String,
        trim: true
      },
      indicator: {
        type: String,
        trim: true
      }
    }
})

Or you can use the subdoc schema.

1565986223
  • 6,420
  • 2
  • 20
  • 33