node.js - mongoose: how query a model with conditions on a property of type reference -
var userschema=new schema({ username: { type: string, required: 'username required' } }); var postingschema=new schema({ creator: { type: schema.objectid, ref: 'user', required:true } });
i want retrieve postings given username. doing in below way, not working.
posting .find({ 'creator.username': req.params.username }) .populate('creator', 'firstname lastname fullname username profile_pic') .exec(function(err, postings){ if (err){ res.status(501).json({ error: err}); } else { res.json(postings); } });
i got empty results array. may wrong query conditons. how apply conditions on ref objects. can correct this?
refer query populate
.populate({ path: 'creator', match: { username: req.params.username })
or
.populate('creator', null, { username: req.params.username })
Comments
Post a Comment