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

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -