node.js - Querying after populate in Mongoose -
i'm pretty new mongoose , mongodb in general i'm having difficult time figuring out if possible:
item = new schema({ id: schema.objectid, datecreated: { type: date, default: date.now }, title: { type: string, default: 'no title' }, description: { type: string, default: 'no description' }, tags: [ { type: schema.objectid, ref: 'itemtag' }] }); itemtag = new schema({ id: schema.objectid, tagid: { type: schema.objectid, ref: 'tag' }, tagname: { type: string } }); var query = models.item.find({}); query .desc('datecreated') .populate('tags') .where('tags.tagname').in(['funny', 'politics']) .run(function(err, docs){ // docs empty });
is there better way this?
edit
apologies confusion. i'm trying items contain either funny tag or politics tag.
edit
document without clause:
[{ _id: 4fe90264e5caa33f04000012, dislikes: 0, likes: 0, source: '/uploads/loldog.jpg', comments: [], tags: [{ itemid: 4fe90264e5caa33f04000012, tagname: 'movies', tagid: 4fe64219007e20e644000007, _id: 4fe90270e5caa33f04000015, datecreated: tue, 26 jun 2012 00:29:36 gmt, rating: 0, dislikes: 0, likes: 0 }, { itemid: 4fe90264e5caa33f04000012, tagname: 'funny', tagid: 4fe64219007e20e644000002, _id: 4fe90270e5caa33f04000017, datecreated: tue, 26 jun 2012 00:29:36 gmt, rating: 0, dislikes: 0, likes: 0 }], viewcount: 0, rating: 0, type: 'image', description: null, title: 'dogggg', datecreated: tue, 26 jun 2012 00:29:24 gmt }, ... ]
with clause, empty array.
what asking isn't directly supported can achieved adding filter step after query returns.
first, .populate( 'tags', null, { tagname: { $in: ['funny', 'politics'] } } )
need filter tags documents. then, after query returns you'll need manually filter out documents don't have tags
docs matched populate criteria. like:
query.... .exec(function(err, docs){ docs = docs.filter(function(doc){ return doc.tags.length; }) // stuff docs });
Comments
Post a Comment