Dynamically adding fields in the schema doesn't permit to add fields as usual
See original GitHub issueHi,
When I dynamically add a field to a schema and instantiate it to a model, mongoose is not aware of any change made on this new field when I call save.
var schema = new mongoose.Schema({
name: String,
firstname: String
});
var person = db.model('persons', schema);
var a = new person();
a.name = "John";
a.firstname = "Doh";
a.save(function(err, savedGuy) {
schema.add({
field1: String
});
// schema is now updated with the field1
var personV2 = db.model('persons', schema);
personV2.findById(savedGuy['_id'], function(err, john) {
// won't work
john.field1 = 'Custom field 1 value';
// will work
john.set ('field1', 'Custom field 1 value');
});
});
I create dynamic but persistent schemas in my project. I can’t fill any data until I restart node. It looks like a bug for me. The purpose of schema.add function should be to avoid the use of set() which seems useful only for arrays properties that can’t be detected by mongoose.
I need right now to intensively manipulating objects which is not pretty at all. So my questions are : is this an issue ? If so, is this possible to think about a fix ?
Best regards and thank you for your work !
Issue Analytics
- State:
- Created 10 years ago
- Reactions:1
- Comments:16
Top Results From Across the Web
Creation of dynamic model fields in django - Stack Overflow
I want to provide a form where the user can add extra fields depending on the automobile that he is adding. For example,...
Read more >Dynamically Adding & Removing Columns with a Collapsible ...
Traditionally, the way to add and remove columns in the Tableau interface is through the Measure Names filter. However, this method can get ......
Read more >A New Service to Allow Users to Create Dynamic Fields in Solr
We designed a static Solr schema.xml file reflecting these decisions with 45 individual fields and 4 copy fields, along with a set of...
Read more >Dynamic Forms Known Issues - Salesforce Help
A record's printable view is based on the fields from its default page layout, not the fields on the Dynamic Forms-based page. Dynamic...
Read more >Schema API | Apache Solr Reference Guide 6.6
Fields, dynamic fields, field types and copyField rules may be added, removed or replaced. Future Solr releases will extend write access to allow...
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
Two solutions:
discriminator()
, you need to pass the same options you passed toCourseSchema
andUserSchema
. You can change thetoJSON
andtoObject
schema options, but notstrict
. Admittedly this is somewhat cumbersome and will likely change in the future (#3414). For more information on discriminators, check out my blog post on discriminators.