Remove duplicates in mongodb find

Viewed 36

I have the following find query, which returns all the products with the matching storeIDs

numberOfUniqueProducts =  await this.productStoreModel.find({store: { $in: storeIDs }}).lean()

However, I want to filter this to remove duplicate products. I have managed to do this in a filter function, however i was wondering if it could be done in the find itself?

let numberOfUniqueProducts = numberOfUniqueProducts.filter(
(v, i, a) => a.findIndex((t) => t.product === v.product) === i
);

Is there a way to do this?

1 Answers

Insted of find you can use this.productStoreModel.distinct({store: { $in: storeIDs }}).

This will give you distinct data.

for more info Click here!

Related