Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is data returned from Mongoose immutable?

I want to add to the return data from a mongoose query:

User.findById(userId, function(err, data) {
  if (!err) {
    data.newvar = 'Hello, world';
  }
});

However, when I console log the output, the newvar does not exist. I've also tried this using Underscore's extend:

_.extend(data, {'newvar': 'Hello, world'});

With no luck either. Since I have nested documents, using a shallow copy won't work. Is there any way to append data here?

like image 245
James Avatar asked Nov 12 '12 19:11

James


People also ask

What does it mean that the data returned by a mongoose API is immutable?

Mongoose 5.6. 0 was released last week. This new release has 12 new features, 2 performance improvements, and several docs improvements. The most interesting new feature is immutable properties. The idea is that marking a property as immutable means that property cannot change after the document is created.

What does mongoose find return?

find() function returns an instance of Mongoose's Query class. The Query class represents a raw CRUD operation that you may send to MongoDB. It provides a chainable interface for building up more sophisticated queries. You don't instantiate a Query directly, Customer.

What is the difference between mongoose schema and model?

Mongoose Schema vs. Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

Is schema necessary for mongoose?

MongoDB is not schema-less. It's got flexible schema - there is a big difference. it's a matter of taste, Mongoose lets you have a typed schema with validations, if you use the driver you don't get that and will have to roll your own.

What is the use of immutable path in mongoose?

This allows for tailored behavior based on options passed in the schema. Defines this path as immutable. Mongoose prevents you from changing immutable paths unless the parent document has isNew: true. Mongoose also prevents changing immutable properties using updateOne () and updateMany () based on strict mode.

What's new in mongoose?

The most interesting new feature is immutable properties. The idea is that marking a property as immutable means that property cannot change after the document is created. One of the primary goals for Mongoose is allowing you to safely write untrusted data to MongoDB without any additional validation.

What is mongoose used for in Node JS?

Many Node.js developers choose to work with Mongoose to help with data modeling, schema enforcement, model validation, and general data manipulation. And Mongoose makes these tasks effortless.

How to bypass mongoose's error messages?

If Mongoose's built-in error message templating isn't enough, Mongoose supports setting the message property to a function. To bypass Mongoose's error messages and just copy the error message that the validator throws, do this: schema.path('name').validate( { validator: function() { throw new Error('Oops!'); }, // `errors ['name']` will be "Oops!"


2 Answers

One way to handle this is to convert your mongoose model instance into a plain object that you have full control over by calling toObject() on it:

User.findById(userId, function(err, data) {
  if (!err) {
    data = data.toObject();
    data.newvar = 'Hello, world';
  }
});

If you want a more structured solution, you can add virtual attributes to your schema as described here.

like image 79
JohnnyHK Avatar answered Sep 22 '22 06:09

JohnnyHK


As it turns out, Mongoose documents are their own special class and not standard Javascript objects. In order to get a javascript option that can be extended, you must use the toObject() method.

like image 25
James Avatar answered Sep 23 '22 06:09

James