0

I have a mongoose schema like this - now the default keyword in url field works but how do i set a similar default value to only id field in the comments section?

var feedSchema = mongoose.Schema({
    url:{type:String, unique:true, default: uuid.v4()},
    comments:[{
        id:String,
        text:String,
        username:String
    }],
});

1 Answer 1

1

Use an object to define the id field so that you can provide a default for it:

var feedSchema = mongoose.Schema({
    url:{type:String, unique:true, default: uuid.v4()},
    comments:[{
        id: {type: String, default: '55'},
        text:String,
        username:String
    }],
});
Sign up to request clarification or add additional context in comments.

2 Comments

How would you define a default value for comments? e.g.: comments will be [] (empty array).
@Deerloper It would be best to post that as a new question.

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.