簡體   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