Node Mongoose:小于,大于查询不起作用

时间:2017-01-25 06:43:52

标签: node.js mongodb filter schema

因为我是mongoose和NodeJS的新手。我被困在Mongoose中查询。

我通过使用大于和小于关键字的价格过滤来获取产品,但我得到错误的值。以下是我的详细信息。

产品架构:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
console.log('productSchema');
var productSchema = new Schema({
    name: {
        type: String,
        required: true,
        trim: true
    },
    sku: {
        type: String,
        required: true,
        unique: true
    },
    price: {
        type: Number,
        required: true
    },
    special_price: {
        type: Number,
        required: false
    },
    description: {
        type: String,
        required: false,
        trim: true
    },
    image: {
        type: String,
        required: false
    },
    galleryimage: {
        type: Object,
        required: false
    },
    stockstatus: {
        type: String,
        required: false
    },
    reviewscount: {
        type: String,
        required: false
    },
    overallrating: {
        type: String,
        required: false
    },
    urlkey: {
        type: String,
        required: false
    },
    shortdescription: {
        type: String,
        required: false,
        trim: true
    },
    productid: {
        type: String,
        required: false
    },
    categoryid: {
        type: String,
        required: false
    },
    reviews: {
        type: Object,
        required: false
    },
    upsell: {
        type: Object,
        required: false
    },
    moreinformation: {
        type: Object,
        required: false
    }
});


module.exports = mongoose.model('products', productSchema);

更改为MyQuery:

Product.find({ price: { $gt: 1.0000, $lt: 120.0000 }}, function(err, products){
            for(var i = 0; i<products.length; i++){
             console.log(products[i].price);
            }
            callback(null, products);
        });

我也试过这段代码

 Product.find(function(err, products){
            for(var i = 0; i<products.length; i++){
             console.log(products[i].price);
            }
            callback(null, products);
        }).where('price').gt(1.0000).lt(120.0000);

这在哪里错了?

编辑: MongoDB数据:

{
    "_id" : ObjectId("587ca7bc5f05ff3280b4756a"),
    "productid" : "820",
    "name" : "Thorpe Track Pant",
    "sku" : "MP07",
    "price" : "120.0000",
    "image" : "http://localhost/magento/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_main.jpg",
    "special_price" : null,
    "description" : "<p>Thirty degree temps are chilly for most, except when you're in Thorpe Track Pants. These top-of-the-line track bottoms are made from fast-drying, weather-resistant fabric with an internal breathable layer of mesh nylon to wick away moisture.</p>\n<p>&bull; Moisture transfer properties. <br />&bull; 7% stretch.<br />&bull; Reflective safety trim.<br />&bull; Elastic drawcord waist.</p>",
    "stockstatus" : "instock",
    "reviewscount" : null,
    "overallrating" : null,
    "galleryimage" : [ 
        {
            "large" : "http://localhost/magento/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_alt1.jpg",
            "thumbnail" : "http://localhost/magento/pub/media/catalog/product/cache//80x/beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_alt1.jpg"
        }, 
        {
            "large" : "http://localhost/magento/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_main.jpg",
            "thumbnail" : "http://localhost/magento/pub/media/catalog/product/cache//80x/beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_main.jpg"
        }, 
        {
            "large" : "http://localhost/magento/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_back.jpg",
            "thumbnail" : "http://localhost/magento/pub/media/catalog/product/cache//80x/beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_back.jpg"
        }, 
        {
            "large" : "http://localhost/magento/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_side_a.jpg",
            "thumbnail" : "http://localhost/magento/pub/media/catalog/product/cache//80x/beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_side_a.jpg"
        }, 
        {
            "large" : "http://localhost/magento/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_side_b.jpg",
            "thumbnail" : "http://localhost/magento/pub/media/catalog/product/cache//80x/beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_side_b.jpg"
        }
    ],
    "reviews" : [],
    "upsell" : [],
    "moreinformation" : [ 
        {
            "value" : "Block after Info Column",
            "label" : "Display Product Options In"
        }, 
        {
            "value" : "Taxable Goods",
            "label" : "Tax Class"
        }, 
        {
            "value" : [ 
                "Cocona&reg; performance fabric", 
                "Polyester", 
                "Rayon", 
                "Wool"
            ],
            "label" : "Material"
        }, 
        {
            "value" : "No",
            "label" : "Eco Collection"
        }, 
        {
            "value" : "No",
            "label" : "Performance Fabric"
        }, 
        {
            "value" : "No",
            "label" : "Erin Recommends"
        }, 
        {
            "value" : "Yes",
            "label" : "New"
        }, 
        {
            "value" : "No",
            "label" : "Sale"
        }, 
        {
            "value" : [ 
                "Sweatpants", 
                "Track Pants", 
                "Workout Pants"
            ],
            "label" : "Style"
        }, 
        {
            "value" : "Solid",
            "label" : "Pattern"
        }, 
        {
            "value" : [ 
                "All-Weather", 
                "Cold", 
                "Cool", 
                "Spring", 
                "Wintry"
            ],
            "label" : "Climate"
        }
    ],
    "categoryid" : "18,32,8,2",
    "__v" : 0
}

1 个答案:

答案 0 :(得分:2)

您的查询是正确的问题是当您在数据库中保存价格时,您要保存一个数字的字符串,$ gt和$ lt适用于不在字符串上的数字

Have a look on your database 

{
    "_id" : ObjectId("587ca7bc5f05ff3280b4756a"),
    "productid" : "820",
    "name" : "Thorpe Track Pant",
    "sku" : "MP07",
    "price" : "120.0000",  // this is a string not a number
    "image" : "http://localhost/magento/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/m/p/mp07-blue_main.jpg",
    "special_price" : null,
    "description" : "<p>Thirty d