mongoose-fill
mongoose-fill copied to clipboard
Fill property in array of subdocs?
Is it possible to fill a property in an array of subdocs?
var mongoose = require('mongoose-fill');
mongoose.connect('mongodb://localhost/ps2test');
var Vehicle = new mongoose.Schema({
vehicle_id: Number,
});
Vehicle.fill('vehicle', function(callback){
callback(null, "flash");
});
var Stats = new mongoose.Schema({,
vehicles: [Vehicle],
});
var Stat = mongoose.model('CharStat', Stats);
var stat = {
vehicles:[{
vehicle_id: 17
}, {
vehicle_id: 8
}]
};
Stat.create(stat, function (err, doc) {
if (err) console.log(err);
console.log(doc);
Stat.findOne({_id: doc._id}).fill('vehicles.vehicle').exec(function (err, event) {
if (err) console.log(err);
console.log(event.toObject({virtuals:true}));
})
});
Results in:
{ _id: 571d119eedf9251039a5277d,
__v: 0,
vehicles:
[ { vehicle_id: 17,
_id: 571d119eedf9251039a5277f,
vehicle: undefined,
id: '571d119eedf9251039a5277f' },
{ vehicle_id: 8,
_id: 571d119eedf9251039a5277e,
vehicle: undefined,
id: '571d119eedf9251039a5277e' } ],
id: '571d119eedf9251039a5277d' }
The fill function is returning undefined, though it should be "flash". Am I doing something wrong? I tried to emulate the test at https://github.com/whitecolor/mongoose-fill/blob/master/test.js#L182
It seems this won't be supported bacause in this case subdocuments (embedded documents) are not model instances.
Would it be possible to access the fills defined on the subdocument's Schema?
I've added support for embedded in 1.2.3, check it.
Brilliant! Works great, thank you so much.
Do you have a gittip or similar?
There is no need for this, glad to be useful ;)
Unfortunately, this doesn't work for fills of more than 2 levels (which I'm sure is indicating my terrible schema design).
var mongoose = require('mongoose-fill');
mongoose.connect('mongodb://localhost/test');
var Vehicle = new mongoose.Schema({
vehicle_id: Number
});
Vehicle.fill('vehicle', function (callback) {
callback(null, "this is a vehicle");
});
var Character = new mongoose.Schema({
char_vehicles: [Vehicle]
});
Character.fill('test', function (callback) {
callback(null, "this is a character");
});
var Alert = new mongoose.Schema({
characters: [Character]
});
var AlertModel = mongoose.model('Test2', Alert);
alertD = {
characters: [{
char_vehicles: [
{vehicle_id: 2}
]
}]
};
var alertFillTest = new AlertModel(alertD),
alertFillVehicle = new AlertModel(alertD);
alertFillTest.fill('characters.test');
alertFillVehicle.fill('characters.test characters.char_vehicles.vehicle');
var alertTestObj = alertFillTest.toObject({virtuals: true}),
alertVehicleObj = alertFillVehicle.toObject({virtuals: true});
console.log('alertFillTest\n', alertTestObj);
console.log('alertFillVehicle\n', alertVehicleObj);
console.log('alertFillVehicle_Character.char_vehicles\n', alertVehicleObj.characters[0].char_vehicles);
produces:
alertFillTest
{ _id: 571ea14b0fe307c420817659,
characters:
[ { _id: 571ea14b0fe307c42081765a,
char_vehicles: [Object],
test: 'this is a character',
id: '571ea14b0fe307c42081765a' } ],
id: '571ea14b0fe307c420817659' }
alertFillVehicle
{ _id: 571ea14b0fe307c42081765c,
characters:
[ { _id: 571ea14b0fe307c42081765d,
char_vehicles: [Object],
test: undefined,
id: '571ea14b0fe307c42081765d' } ],
id: '571ea14b0fe307c42081765c' }
alertFillVehicle_Character.char_vehicles
[ { vehicle_id: 2,
_id: 571ea14b0fe307c42081765e,
vehicle: undefined,
id: '571ea14b0fe307c42081765e' } ]
You can also see by the characters[0].test field that the other fills fail on a fill call that includes a fill field with more than two levels.
Yes I've seen that but din't find what is wrong there and how can be fixed yet .
I think it's in the addFills method, around https://github.com/whitecolor/mongoose-fill/blob/master/index.js#L96
But I can't reason through it to see how it should be changed.
Maybe this slicing https://github.com/whitecolor/mongoose-fill/blob/master/index.js#L101
Well if you will be capable of fixing will be great, I will check this a little bit later) Yous this https://github.com/whitecolor/cycle-mongoose better)
I'll spend some time tonight working through it and see what I can do.
Cycles looks interesting, but it's too late in my current project to switch like that.