Lilith
Lilith

Reputation: 437

Is there a way to fetch all unique array values from a mongoose schema?

All of my mongoose blog posts have a array of tags, is there a way to fetch all unique tags from those arrays?

Blog Schema Excerpt

{
...
  tags: {
    type: [{type:String,lowercase:true}],
    default: [],
    required: true
  }
...
}

Upvotes: 1

Views: 86

Answers (1)

Danziger
Danziger

Reputation: 21161

Use distinct:

Post.distinct('tags', (err, result) => {
    if (err) ...;

    console.log('UNIQUE TAGS:', result);
});

Upvotes: 2

Related Questions