mongoose-fill icon indicating copy to clipboard operation
mongoose-fill copied to clipboard

Fill property in array of subdocs?

Open eanmclaughlin opened this issue 9 years ago • 11 comments

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

eanmclaughlin avatar Apr 24 '16 18:04 eanmclaughlin

It seems this won't be supported bacause in this case subdocuments (embedded documents) are not model instances.

wclr avatar Apr 25 '16 09:04 wclr

Would it be possible to access the fills defined on the subdocument's Schema?

eanmclaughlin avatar Apr 25 '16 14:04 eanmclaughlin

I've added support for embedded in 1.2.3, check it.

wclr avatar Apr 25 '16 15:04 wclr

Brilliant! Works great, thank you so much.

Do you have a gittip or similar?

eanmclaughlin avatar Apr 25 '16 15:04 eanmclaughlin

There is no need for this, glad to be useful ;)

wclr avatar Apr 25 '16 15:04 wclr

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.

eanmclaughlin avatar Apr 25 '16 23:04 eanmclaughlin

Yes I've seen that but din't find what is wrong there and how can be fixed yet .

wclr avatar Apr 27 '16 19:04 wclr

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.

eanmclaughlin avatar Apr 27 '16 19:04 eanmclaughlin

Maybe this slicing https://github.com/whitecolor/mongoose-fill/blob/master/index.js#L101

eanmclaughlin avatar Apr 27 '16 19:04 eanmclaughlin

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)

wclr avatar Apr 27 '16 20:04 wclr

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.

eanmclaughlin avatar Apr 27 '16 20:04 eanmclaughlin