2017-01-25 138 views
0

由於我是新來的貓鼬和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 
} 
+0

您的查詢看起來很好,它適用於我。也許你可以發佈一些你期望的樣本數據和你正在獲取的樣本數據。 –

+0

@node_saini,當然和完成 – Charlie

+0

在架構中,您的價格數據類型是數字,但在您的數據價格是字符串,因此它不會相應地工作。 [請參閱本文](http://stackoverflow.com/q/18039358/4394926),並在這個主題上有幾個帖子,引用他們,你會得到你的預期結果。 –

回答

2

您的查詢是正確的問題是,當你要保存的價格在你的數據庫要保存一個字符串insted的編號和$ 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