Reputation: 14399
I have this schema articleSchema:
{
//other attributes
tags : [ String ]
}
I want to search for articles based on a certain criteria and retrieve only the tags, and then create a single array of the tags from all the articles without duplicates.
Is there any built in functionality in mondgodb and mongoose of doing this?
Upvotes: 0
Views: 775
Reputation: 749
Try it.
db.getCollection('collection').find({'your query'}).distinct('tags', function(err, results){
console.log(results);
});
Upvotes: 0
Reputation: 14399
As pointed out by WiredPrairie, distinct
was the solution.
var query = { /** Query for the articles that I want tags from */ };
//Using mongoose-q
return Article.distinctQ('tags', query);
Upvotes: 1