JenuRudan
JenuRudan

Reputation: 545

Mongoose populate model with strict false

I've got a mongoose model as follows:

var mongoose = require('mongoose');
var timestamps = require('goodeggs-mongoose-timestamps');
var Schema = mongoose.Schema;

var BeatSchema = new Schema({
    code: {
        type: String
    },
    removed: {
        type: Boolean,
        default: false
    }

},
{
    strict: false
});

BeatSchema.plugin(timestamps);
module.exports = mongoose.model('Beat', BeatSchema);

With strict:false I can insert any attributes that aren't in the model schema and they are saved into database, the issue is that sometimes I insert attributes of objectIDS to other schema. So how can I populate these documents ?

example:

var params = {employee: employee._id, action: 'logout,code:"DL2"};
var beat = new Beat(params);
await beat.save();

If I use Beat.find({code:"DL2"}) I will get employee as ID only, so how can I populate this model during Beat.find ?

Upvotes: 0

Views: 4775

Answers (1)

JenuRudan
JenuRudan

Reputation: 545

I've found the answer, just add virtual attribute to the model and populate by it's name

BeatSchema.virtual('pEmployee', {
  ref: 'Employee',
  localField: 'employee',
  foreignField: '_id'
});

Upvotes: 1

Related Questions