Categories
JavaScript Answers

How to query for distinct values in Mongoose and Node?

Spread the love

To query for distinct values in Mongoose and Node, we call the distinct method.

For instance, we write

MyModel.find().distinct("_id", (error, ids) => {
  // ...
});

to call distinct to return an array of distinct values.

We get the entries that have distinct _id values.

And then we get the _id values from ids in the callback.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *