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.

Ability to add properties that aren't defined on the schema

See original GitHub issue

I have a use case where I read some documents from the database and then do some calculations on their data, after which I’d like to append this data to the documents as new properties.

In Mongoose, this is not possible unless the properties are defined in the schema. E.g. Mongoose will not allow you to add unknown properties that you haven’t defined on the schema in advance.

So the “normal” solution would be to convert the documents to plain objects or JSON with toObject or toJSON first.

This works fine in most cases, but in my specific case I have defined several methods on the document model as well, and I’d like to retain the ability to call them for as long as I can. Converting the documents to plain objects or JSON obviously discards the methods, making it impossible to use them further down the data processing chain.

The only apparent work around would be to change the order of data processing or store both the models and JSON objects separately, but this is a bit of a drag if you’re passing data around via promises or the req object.

I’m wondering if it would be hard to implement a new flag for Mongoose schema’s which when enabled, will in fact let you add “new” or unknown properties to a document. This data can be ignored when a document is saved or validated, but appended when a document is converted to JSON or plain object.

Another use case for it that I encountered is to append some temporary meta data to my documents. Again, with the data processing example, I’d like to maybe find some additional data/information about my documents, and attach this to them temporarily for later use, again without losing access to my document methods.

Thoughts?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

17reactions
adamreisnzcommented, Oct 24, 2016

I don’t think they do, I have had to access them via document._doc.someProperty, because they wouldn’t get attached to the actual model based document, and as a result, also didn’t show up in toJSON() output. I assumed this is a feature of Mongoose. E.g.:

Model
  .findOne({...})
  .then(document => {
    document.nonSchemaProperty; //undefined
    document._doc.nonSchemaProperty; //defined
  });
4reactions
vkarpov15commented, Nov 25, 2016

Nope it’s perfectly valid for a virtual to set a property that isn’t on the schema (modulo strict mode).

var personSchema = new Schema({
  name: String
});

personSchema.virtual('test').
  get(function() { return this._test; }).
  set(function(v) { this._test = v; });

var Person = mongoose.model('Person', personSchema);

var p = new Person({ name: 'Val', test: '123' });

console.log(p.toObject({ virtuals: true }));
$ node gh-4614.js 
{ name: 'Val',
  _id: 5838cc4ccf01b36af4082291,
  test: '123',
  id: '5838cc4ccf01b36af4082291' }
^C
$ 

With the default strict mode, you can set whatever properties on the object you want, they just won’t get persisted to the db.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to add fields not described in Mongoose model?
You can use type 'Schema.Types.Mixed' for some field in your schema: let ...
Read more >
Is there a way to add objects that we do not define in the ...
I want to add objects that I don't define in the schema to my database. from pymongo import MongoClient from collections import OrderedDict ......
Read more >
Add custom data to resources using extensions - Microsoft Learn
First, you create your schema extension definition. Then, use it to extend supported resource instances with strongly-typed custom properties.
Read more >
Resource type schema - CloudFormation Command Line ...
A type configuration schema that defines any properties that the user must specify for all instances of the extension in a given account...
Read more >
Mongoose Schema Basics
I'm rejecting it, and won't be adding it to the database. ... to ensure that our users aren't submitting junk data into our...
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