Reputation: 3793
I got stuck at populating my array... this is what i get from my Terminal.find()
function:
{ _id: 5a7300046f18140d5081be83,
type: 5a71a22b0d4a2d0ec3b91edc,
serial: '213',
macAddress: 'E1:12:BE:82:12',
barcode: '1243w464563',
dateArrival: 2018-02-01T11:54:15.870Z,
firmware: 5a71a2030d4a2d0ec3b91edb,
installedAt: 5a61e547922c6d28e324d775,
installedAtBranch: 5a7054713fe20c0c5a94fb11,
__v: 0,
deleted: false,
activated: true }
So i want to get the correct information, when a user searches for the terminal. This is why i have to use populate()
. Difficult for me now is to populate things deeper inside the array. So the first Object type
has a readMethod
, a layout
and interfaces
- i have to populate those three, but i don't know how. This is aggravated by the fact that interfaces
is an Object inside my Database. So my type
looks like this:
I think if someone can show me how to populate type
with all of its interfaces
i can achive the rest on my own.
Thanks in advance!
I found the answer on my own by searching deeper inside google:
Terminal.findOne({'barcode': barcode})
.populate({
path: 'type',
populate: [{
path: 'user',
model: 'User'
},
{
path: 'method',
model: 'Method',
},
{
path: 'layout',
model: 'Layout'
},
{
path: 'interfaces',
model: 'Interface'
}]
})
.populate('firmware')
.populate('customerId')
.populate('branchId')
.exec( function (err, terminal) {
if (err) {
res.status(500).send(err);
}
res.json({ terminal });
});
Upvotes: 0
Views: 698
Reputation: 6160
As you are using mongoose then I think your model should look like following or it must be like following
Terminal Type Schema
var terminalTypeSchema = new Schema({
articlenumber: { type: Number, default: something },
readMethod: Schema.ObjectId,
layout: Schema.ObjectId,
interfaces: [{ type: Schema.ObjectId, ref: 'User' }]
});
mongoose.model('TerminalType', terminalTypeSchema);
Interface Schema
var interfaceSchema = new Schema({
// some vars
});
mongoose.model('Interface', interfaceSchema);
Now run following query
TerminalType.findOne({ _id: terminalTypeId})
.populate('interfaces') // <==
.exec(function(err, terminalTypes){
// You can achieve the rest on your own.
});
Upvotes: 1