繁体   English   中英

$gte & $lte 在 Mongoose/MongoDB 中没有按预期工作

[英]$gte & $lte is not working as expected in Mongoose/MongoDB

我正在按价格范围过滤产品列表。 有些价格范围很好,有些则不行。 当我单击(单选框)前端的价格范围时,它会通过价目表在后端进行处理。 看起来 $gte 和 $lte 没有按预期工作。 我错过了什么吗?

反应前端----价格表:

export const prices = [
    {
        _id:0,
        name: 'Any',
        array: []
    },
    {
        _id:1,
        name: '$50 to $99',
        array: [50, 99]
    },
    {
        _id:2,
        name: '$100 to $199',
        array: [100, 199]
    },
    {
        _id:3,
        name: '$200 to $299',
        array: [200, 299]
    },
    {
        _id:4,
        name: '$300 to $399',
        array: [300, 399]
    },
    {
        _id:5,
        name: '$400 to $599',
        array: [400, 599]
    },
    {
        _id:6,
        name: '$600 to $799',
        array: [600, 799]
    },
]

NodeJS后端--------产品搜索列表:

exports.listBySearch = (req, res) => {
    let order = req.body.order ? req.body.order : "desc";
    let sortBy = req.body.sortBy ? req.body.sortBy : "_id";
    let limit = req.body.limit ? parseInt(req.body.limit) : 100;
    let skip = parseInt(req.body.skip);
    let findArgs = {};

    // console.log(order, sortBy, limit, skip, req.body.filters);
    // console.log("findArgs", findArgs);

    for (let key in req.body.filters) {
        if (req.body.filters[key].length > 0) {
            if (key === "price") {
                // gte -  greater than price [0-10]
                // lte - less than
                findArgs[key] = {
                    $gte: req.body.filters[key][0],
                    $lt: req.body.filters[key][1]
                };
                console.log(findArgs)
               // console.log -- { price: { '$gte': 50, '$lt': 99.99 } }
            } else {
                findArgs[key] = req.body.filters[key];
            }
        }
    }

    Product.find(findArgs)
        .select("-images")
        .populate("category")
        .sort([[sortBy, order]])
        .skip(skip)
        .limit(limit)
        .exec((err, data) => {
            if (err) {
                return res.status(400).json({
                    error: "Products not found"
                });
            }
            res.json({
                size: data.length,
                data
            });
        });
};

Mongoose --------- 数据库:

{
    "_id" : ObjectId("6079a8d675db7021c00aa973"),
    "sold" : 5,
    "name" : "Java Book for Everyone",
    "description" : "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
    "price" : "75",
    "category" : ObjectId("605c9c94dcf90b34e8ec33e0"),
    "shipping" : false,
    "quantity" : 7,
    "createdAt" : ISODate("2021-04-16T15:10:14.626Z"),
    "updatedAt" : ISODate("2021-04-16T15:10:14.626Z"),
    "__v" : 0
}

mongoDB 文档中的价格是string类型。 这就是为什么查询不起作用。 您想将 mongo 中的 price 值转换为 number 以进行比较。 修改查询以发送如下内容:

 { $expr: { $and: [ $gte: [{ $toDouble: "$price" }, req.body.filters[key][0]], $lt: [{ $toDouble: "$price" }, req.body.filters[key][1]] ] } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM