1

I'm newbie to mongodb Here is my schema

import mongoose, { Schema } from 'mongoose';

const SomeSchema = new Schema({

  vDocs: [{type: String , required: true,  }],
  vBelongsTo: { type: Schema.Types.ObjectId, ref: 'User' }

});

const SomeSchema = mongoose.model('BlaBla', SomeSchema);
export default SomeSchema;

mongoose is saving only simple array like ["bla", "bla"] in vDocs

but I want to save something like this [{key: val}, {key: val}] in vDocs Both are array I don't why is not working

code.king
  • 368
  • 1
  • 3
  • 10
  • Possible duplicate of [How to define object in array in Mongoose schema correctly with 2d geo index](https://stackoverflow.com/a/19701983/1207049) – marekful Feb 02 '18 at 07:03

2 Answers2

2

You have declared type to be a string but you are trying to save an object

 //Try This
 let newObj = new SomeSchema
 newObj.vDocs = JSON.stringify(whatEverObject)
 SomeSchema.save().then(function(v){
        // whatever 
    })
ashwintastic
  • 2,262
  • 3
  • 22
  • 49
0

I had the same problem, I solved it by changing type: Array. The following should work:

vDocs: [{ type: Array, required: true }],
Josef
  • 2,869
  • 2
  • 22
  • 23
PRIS54
  • 21
  • 4