I have a mongoose database schema which is something like -
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Discussion Question Schema
*/
var QuerySchema = new Schema({
date: {
type: Date,
default: Date
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
class: {
type: Schema.ObjectId,
ref: 'Class'
}
});
module.exports = mongoose.model('Query', QuerySchema);
and now I want to populate some fields of the user schema and class schema and not all of them so how do I do it in an express controller
var query = Query.find({}).populate('user','name email avatar');
query.paginate(options,function(err, resp){
if(err) { return handleError(res, err); }
console.log(resp);
return res.status(200).json(resp.results);
});
so I am populating only 'name email avatar'
of the 'user'
schema but I also want to populate some of the fields of 'class' schema like 'name, strength'
I have tried the following ways for this but haven't got the desired response from server -
var query = Query.find({}).populate('user class','name email avatar');
var query = Query.find({}).populate('user','name email avatar class');
[UPDATE]
I am getting the following error on adding class
to the populated field list
E:\work\01India\applications-tests\themeforest-8437259-angular-bootstrap-admin-web-app-with-angularjs\node_modules\mongoose\lib\connection.js:660
throw new MongooseError.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Class".
Use mongoose.model(name, schema)
at NativeConnection.Connection.model (E:\work\01India\applications-tests\themeforest-8437259-angular-bootstrap-admin-web-app-with-angularjs\node_modules\mongoose\lib\connection.js:660:11)
at getModelsMapForPopulate (E:\work\01India\applications-tests\themeforest-8437259-angular-bootstrap-admin-web-app-with-angularjs\node_modules\mongoose\lib\model.js:2366:26)
at populate (E:\work\01India\applications-tests\themeforest-8437259-angular-bootstrap-admin-web-app-with-angularjs\node_modules\mongoose\lib\model.js:2207:15)
at Function.Model.populate (E:\work\01India\applications-tests\themeforest-8437259-angular-bootstrap-admin-web-app-with-angularjs\node_modules\mongoose\lib\model.js:2177:5)
at Immediate.cb (E:\work\01India\applications-tests\themeforest-8437259-angular-bootstrap-admin-web-app-with-angularjs\node_modules\mongoose\lib\query.js:1011:16)
at Immediate._onImmediate (E:\work\01India\applications-tests\themeforest-8437259-angular-bootstrap-admin-web-app-with-angularjs\node_modules\mongoose\node_modules\mquery\lib\utils.js:137:16)
at processImmediate [as _immediateCallback] (timers.js:374:17)
My class schema is as follows -
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Class Schema
*/
var ClassSchema = new Schema({
date: {
type: Date,
default: Date
},
class: {
type: String,
enum: ['10','12']
},
subjects: [{
type: Schema.ObjectId,
ref: 'Subject'
}]
});
module.exports = mongoose.model('Class', ClassSchema);
and the contents of my classes collection when I type the following query in mongo shell are -
db.classes.find();
output of this is -
{ "_id" : ObjectId("5635f365e21a4b9015904e08"), "subjects" : [ "5635f365e21a4b9015904e08", "56360f5029a82c0823eac2b8" ], "date" : ISODate("2015-11-01T11:11:33Z"), "__v" : 0, "class" : "10" }
Chain two populate
calls together so that you can set the options of each field separately:
var query = Query.find({}).populate('user','name email avatar')
.populate('class','name strength');