i have to populate a result json , from mongodb, using mongoose , in my case i am dealing with two collections named 'portfolio' and 'portfoliogroup'. I stored many rows in portfolio based on group selection. Now need to populate a data set with group name instead of group id.
//portfolio.js - model file
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var portfoliolistSchema = new Schema({
title: String,
desc: String,
img: String,
portgroup: [{ type: mongoose.Schema.Types.ObjectId, ref: 'portfoliogroup' }]
});
mongoose.model('portfoliolist', portfoliolistSchema);
//portfolio group model
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var portfolioGroupSchema = new Schema({
name: String,
});
mongoose.model('portfoliogroup', portfolioGroupSchema);
//express route
app.get('/pp', function (req, res) {
var plist = [];
portfoliolist.find().populate("portfoliogroup").exec(function (err, lists) {
res.json(lists)
});
})
When i access the route im getting the following result json
[
{
"_id":"5511add1ba3221501f511788",
"title":"to dodddd",
"desc":"ddfsdfsdf",
"img":"http://localhost:3000/media/336a1427221953256.jpg",
"__v":0,
"portgroup":[
"5511a88906dc9ef4074e0827"
]
},
{
"_id":"551646dfbe25faa01af8e9ac",
"title":"New portfolio",
"desc":"Small Desc",
"img":"http://placehold.it/200x200",
"__v":0,
"portgroup":[
"5511a88906dc9ef4074e0828"
]
}
]
but i need result josn should come with group name instead of group id
[
{
"_id":"5511add1ba3221501f511788",
"title":"to dodddd",
"desc":"ddfsdfsdf",
"img":"http://localhost:3000/media/336a1427221953256.jpg",
"__v":0,
"portgroup":[
"Website"
]
},
{
"_id":"551646dfbe25faa01af8e9ac",
"title":"New portfolio",
"desc":"Small Desc",
"img":"http://placehold.it/200x200",
"__v":0,
"portgroup":[
"ghraphic"
]
}
]
My environment is nodejs, expressjs,mongoose
any solution for this?