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

1

2 Answers 2

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 
    })
Sign up to request clarification or add additional context in comments.

1 Comment

it worked, but when I have to retrive I have to use JSON.parse()
0

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

vDocs: [{ type: Array, required: true }],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.