-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Advanced custom schema type for custom objects or value-objects #4356
Comments
|
Thank you for the reply!
Yes, it work. But the debug logs of Mongoose seem to don't call it and shows it wrong (object instead of array):
No, I tried to call the toObject() on the main model and it don't call it on the custom type. |
Ok will make sure that debug logs handle |
Thank you! |
I added the toBSON() check in 1a93d1f. Here's a better example of how to do the polygon type you're looking at: 'use strict';
var assert = require('assert');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/gh4356');
mongoose.set('debug', true);
class Polygon {
constructor(v) {
this.v = v;
}
toBSON() {
return this.v;
}
}
class PolygonSchema extends mongoose.SchemaType {
cast(v) {
return new Polygon(v);
}
}
mongoose.Schema.Types.Polygon = PolygonSchema;
const schema = new Schema({ test: Polygon });
const M = mongoose.model('Test', schema);
M.create({ test: 5 }).then(doc => {
console.log('done', doc);
console.log(doc.test.toBSON());
process.exit(0);
}); Output:
|
I couldn't find any example for storing a composite object in MongoDB using Mongoose and retrieving the inner object with all the methods without typecasting. I already posted a question on StackOverflow: Please help. |
@NikhilAshodariya I opened up a separate issue for this #6174. |
I couldn't find any example of an advanced custom schema type involving custom objects (or value-objects) in Mongoose >=4.4.
I already posted a question on StackOverflow without success: http://stackoverflow.com/questions/38094817/mongoose-advanced-custom-schema-object-type.
Resuming, how can I achieve that:
SchemaType.prototype.cast
function.encoded and stored in a custom way (as plain object, array, string or any BSON type).
model.toObject()
it will recursively call themodel.myfield.toObject()
to have a full plain object representation of the document.The text was updated successfully, but these errors were encountered: