question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamically adding fields in the schema doesn't permit to add fields as usual

See original GitHub issue

Hi,

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:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:16

github_iconTop GitHub Comments

14reactions
lijinmacommented, Mar 26, 2014

Two solutions:

  1. Use Mixed types as @aheckmann metioned above;
  2. Mongoose Strict Disable the strict option, (enabled by default), ensures that values added to our model instance that were not specified in our schema do not get saved to the db. NOTE: do not set to false unless you have good reason.
    var thingSchema = new Schema({..}, { strict: false });
    var thing = new Thing({ iAmNotInTheSchema: true });
    thing.save() // iAmNotInTheSchema is now saved to the db!!
3reactions
vkarpov15commented, Sep 28, 2015
  1. Adding paths to the model’s schema after the model has been compiled is unsupported. Please don’t do that.
  2. strict mode
  3. When you use discriminator(), you need to pass the same options you passed to CourseSchema and UserSchema. You can change the toJSON and toObject schema options, but not strict. 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.
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found