0

How to insert array and normal variable data to mongoose database..

var parent = new Parent({ children: [{ name: 'Matt' }, { name: 'Sarah' }], class: 10 })
parent.save(callback);

This is the method i know currently. I need it to be done from the req.body. So how can I done after creating the parent object. ie

var parent =  new Parent();
///code for inserting the array data and other normal datatypes 
parent.save(callback);

1 Answer 1

1

Use the document instance like any other javascript object.

parent.children = [{ name: 'Matt' }, { name: 'Sarah' }];
parent.class = 10;
parent.save();

Just don't change it entirely (like doing parent = {...}), otherwise you'd have de-referenced the actual mongoose document instance. Only make changes on its properties like shown above.

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

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.