Schema, array, default value (default value "[]" is not working)
See original GitHub issuevar mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/dbTest');
var arrayTestSchema = new Schema({
anArray: {
type: Array,
'default': []
}
});
mongoose.model('ArrayTest', arrayTestSchema);
ArrayTest = mongoose.model('ArrayTest');
var myTest = new ArrayTest();
console.log(arrayTestSchema.anArray);
mongoose.connection.close();
displays “undefined”
Issue Analytics
- State:
- Created 13 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Why doesn't my schema to add default values in mongoose ...
Default values really don't work with arrays, unless of course it is a document within the array and you want to set a...
Read more >Default value for nested arrays - Working with Data - MongoDB
is it possible in MongoDB to have default values in nested arrays or objects? I use the following schema for a nested object:....
Read more >Specify default column values | BigQuery - Google Cloud
You can compose a STRUCT or ARRAY default value with these functions, ... The default value for an array cannot be NULL or...
Read more >Mongoose v6.8.1: Defaults
Your schemas can define default values for certain paths. ... Note: Mongoose only applies a default if the value of the path is...
Read more >Initial Value Templates - Sanity.io
By default, whenever you create a new document in the studio, ... project schema type is given the value false for the isHighlighted...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s failing because you’re not accessing your document instance. I’ve commented out your console.log and replaced it with the correct one.
@bnoguchi what about the type of array items?