【问题标题】:Mongoose .find() with unique filterMongoose .find() 具有独特的过滤器
【发布时间】:2022-02-16 18:45:55
【问题描述】:

我有一个包含一些重复项目的大数据库。我可以将 .find() 与唯一字段过滤器一起使用吗?如果我使用 .distinct('term') 它会返回一个唯一术语的数组,例如 ['term1', 'term2', 'term3'],但我想返回一个包含 db 中的整个对象的数组,由唯一的 'term 过滤'。

【问题讨论】:

标签: node.js mongodb mongoose mongodb-query


【解决方案1】:

我通过过滤来自 .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);
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2016-10-30
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    相关资源
    最近更新 更多