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

Populate Two collection by reftype

Open lags opened this issue 10 years ago • 6 comments

My question about retrieve data by two collections with declare type and ref into schema. example my schema:


  var controlunitSchema = new mongoose.Schema({
    osver: String,
    sensors: [{
      mac: { type: String, unique: true },
      alias: { type: String, default: 'SENSOR' },
      status: String,
      type: { type: mongoose.Schema.Types.ObjectId, ref: 'Sensor' }
    }]
  });

and Sensor Schema:

  var sensorSchema = new mongoose.Schema({
    id: { type: String, unique: true },
    description: String,
    timeCreated: { type: Date, default: Date.now },
    timeRegistered: { type: Date, default: Date.now }
  });

Now execute this code for fill my datatable:

  var options = {};
  options.populate = {
    ['sensors.type']
  };
  app.db.models.ControlUnit.dataTable(req.query, options, function (err, data) {
    if (req.xhr) {
      res.send(data);
    }
  });

but object sensors don't populate with id and description data. If watch debug information in my console return:

...
sort: { osver: 'asc' },
  conditions: undefined,
  populate: [] }
Data: { sEcho: '1',
...

populate object don't exsist. It's possible resolve this problem?? Thanks

lags avatar Feb 06 '15 18:02 lags

Hi,

Thank you for your feedback.

I see you are using 'options.populate' on the dataTable function but there is no populate option. I added fields and models to the test (in test folder of this module) and the way you are building sensors field is not working with the actual implementation. If you use sub-document to do the same then it is working... I believe maybe there is a limitation in mongoose...

You can transform your model to achieve your needs:

 var sensorSchema = new mongoose.Schema({
    id: { type: String, unique: true },
    description: String,
    timeCreated: { type: Date, default: Date.now },
    timeRegistered: { type: Date, default: Date.now }
  });
 mongoose.model('Sensor', sensorSchema);

var SensorSubDocSchema = new mongoose.Schema({
      mac: { type: String, unique: true },
      alias: { type: String, default: 'SENSOR' },
      status: String,
      type: { type: mongoose.Schema.Types.ObjectId, ref: 'Sensor' }
});

var controlunitSchema = new mongoose.Schema({
    osver: String,
    sensors: [ SensorSubDocSchema ]
  });
mongoose.model('ControlUnit', sensorSchema);

/* Data fetch */
req.query.mDataProp_1 = 'types.type'
app.db.models.ControlUnit.dataTable(req.query, function (err, data) {
    if (req.xhr) {
      res.send(data);
    }
  });

Tell me if it works for you !

eherve avatar Feb 06 '15 20:02 eherve

Hi eherve, thanks for reply. Unfortunately not work for me. In your code: req.query.mDataProp_1 = 'types.type' i've substitute with req.query.mDataProp_1 = 'sensors.type' but with us i've only access to object type ex: id, description and two date time but: mac, alias and status not exist. Must I declare more field?? ex: req.query.mDataProp_1 = 'sensors.type' for get same data req.query.mDataProp_2 = 'sensors' for get another data

Thanks in advance

lags avatar Feb 10 '15 17:02 lags

Ref to docs at: mongoose populate it's possible add this feature by options? ex: options.populate? Should be very great!! :+1:

lags avatar Feb 10 '15 17:02 lags

Hi,

The module load only what you are requesting nothing more. So you need to specify each data you want to fetch. You need to add all the field you want in your request.

Regarding populate by option, you don't need it since the request of underlying fields will trigger the populate you need.

Regards,

eherve avatar Feb 21 '15 08:02 eherve

@lags have you find a solution to your issue? I'm facing a similar problem

alonsowize avatar Mar 31 '18 00:03 alonsowize

@eherve i'm having the same problem any solutions?

rymesaint avatar Jul 17 '18 11:07 rymesaint