Ludwig Magnusson
Ludwig Magnusson

Reputation: 14399

Mongoose: Find all tags for a specific set of articles

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

Answers (2)

Janen R
Janen R

Reputation: 749

Try it.

  db.getCollection('collection').find({'your query'}).distinct('tags', function(err, results){
              console.log(results);
  });

Upvotes: 0

Ludwig Magnusson
Ludwig Magnusson

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

Related Questions