1

I would like to specify a custom transform of the JSON received by Mongoose as part of a query into a Javascript object. Typically, JSON.parse() or something similar is used. I would like to use my own transform function because I would like to include a new field called __length which is the length of the JSON received over the network. I do not want to use JSON.stringify(document_from_mongo).length() because I am performing an unnecessary stringify operation.

I have investigated using a custom toObject() or toJSON() method on the schema, but have not had any success. Thanks!

4
  • Well what you say that you "don't want to do" is unfortunately the only place you get the string "length" if you want to inject it into the returned object, since you need to stringify in order to determine that. To me it sounds somewhat redundant, as the Content-Length should be set in the headers ( unless streaming output ) and anything reading that string length from inside the object would of course need to "parse" the object first, which limits the utility of such data. Commented Aug 5, 2015 at 2:25
  • Mongoose uses a streaming interface. Commented Aug 5, 2015 at 22:43
  • No it does not. By default mongoose methods such as .find() and .aggregate() return the data as an array of data or a single object in the findBy** varieties. For an output stream you need to specifically call .stream() ( on find, cursor options for aggregate ) or call the native driver methods with much the same thing. So it is not a stream, but a plain object unless you ask otherwise. Even if you asked for a stream, what you are asking for is the "string length" of the JSON emitted. You cannot get that without stringifying the object or stream. Same points about utility noted above. Commented Aug 5, 2015 at 23:29
  • Sounds good, I have a limited understanding of Mongooses internals so I will dig in more. I do think you can get the response length without stringifying because it already arrives from the DB server that way. Using Content-Length would be perfect for what we need though so I'll look into that after I try the other solution posted. Thx! Commented Aug 6, 2015 at 1:47

1 Answer 1

1

You can set your own implementation of toJSON at schema level like the following:

var mySchema = new Schema({});

mySchema.set('toJSON', {
    transform: function(doc, ret, options) {
        ret.__length = 'set what do need';
        return ret;
    }
});

mongoose.model('mySchema', mySchema );
Sign up to request clarification or add additional context in comments.

1 Comment

It appears that doc/ret are both JS objects by the time the transform function is called. I also tried mySchema.methods.toJSON = function(), but both time I cannot call length on the object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.