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.

2
  • Use JSON.stringify(body) Commented Mar 29, 2019 at 12:55
  • You mean an "embedded array". See also Subdocuments in the mongoose documentation for other examples of defining an embedded array schema Commented Mar 29, 2019 at 13:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.