'Mongoose .find() with unique filter

I have a big DB with some duplicate items. Can I use .find() with filter by unique field? If I use .distinct('term') it return an array of unique terms like ['term1', 'term2', 'term3'], but I want to return an array with entire objects from db, filtered by unique 'term'.



Solution 1:[1]

I resolved it by filtering the response from .find()

responses = await Product.find({ product_id: data.input });

    let container = [];

    responses
        .filter((value, index, self) => {
            return (
                self.findIndex((v) => v.search_term === value.search_term) === index
            );
        })
        .map((ele) => {
            container.push(ele);
        });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ovidiu96