I have already created two schema for mongoose named associates and outlets.
Outlet schema:-
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var OutletSchema = new Schema(
{
associate_id: {type: ObjectId, max: 100 },
city_id:{type:ObjectId, max: 100},
outlet_name: {type: String, max: 100},
oulet_address: {type: String, max: 500},
outlet_contact: {type: String, minlength: 9, maxlength: 12 },
active: {type: Boolean}`enter code here`
}`
);
module.exports = mongoose.model('Outlet', OutletSchema);
Associate Schema:-
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
var AssociateSchema = new Schema(
{
associate_name: {type: String, max: 100},
associate_address: {type: String, max: 500},
associate_contact: {type: String, minlength: 9, maxlength: 12 },
associate_email: {type: String, max: 100},
active: {type: Boolean}
}
);
module.exports = mongoose.model('Associate', AssociateSchema);
these are my schema which i created.
How can i populate outlet in associates using relationship()
i have tried this code which is given below.
router.get('/getUsingPopulate', function (req, res) {
data = {}
Associate.relationship({path:'outlet', ref:'Outlet',refPath:'associate_id'})
Outlet.find({}).populate('Associate').exec()
.then(function(alldata){
console.log(alldata)
})
})
database of Associate:-
_id: 5b40428088e5f0187dedb342
category_id: 5b34a99709d3ff3e2b5a399c
associate_name: "Cafe Coffee Day"
associate_address: "Coffee day kormangala"
associate_contact: "989545458645"
associate_email: "ccdkor@yahoo.com"
active: true
__v: 0
database of outlet:-
_id: 5b4471f4c5bf8018409c7c64
associate_id: 5b40428088e5f0187dedb342
city_id: 5b3c49fee0de9210a60a2266
outlet_name: "Coffee Day Whitefield"
oulet_address: " Coffee Day Whitefield"
outlet_contact: "7587454478"
active: true
__v:0
Expecting output:-
{
"_id": "5b40428088e5f0187dedb342",
"name": "Cafe Coffee Day",
"outlets": [
{
"_id": "5b4471f4c5bf8018409c7c64",
"associate_id": 5b40428088e5f0187dedb342,
"name": "Coffee Day Whitefield",
}]
For more info please click herehttps://stackoverflow.com/q/51280063