In Mongoose, the Model class provides the distinct() method to get all the unique values for a specific field.

Here is an example that demonstrates how to use the distinct() method to find unique URLs:

const mongoose = require('mongoose')

const Link = mongoose.model('Link', { url: String, clicks: Number })

const urls = await Link.distinct('url')

The distinct() method also accepts a filter object to restrict unique results:

const urls = await Link.distinct('url', { clicks: { $gt: 100 } })

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.