2017-01-25 7 views
0

Da bin ich neu zu den Mungo und NodeJS. Ich bin mit der Abfrage in Mongoose stecken.Knoten-Mungo: weniger als, größer als Abfrage funktioniert nicht

Ich bin ein Produkt durch Filtern Preis mit mehr als und weniger als Keywords, aber ich bekomme falsche Werte. Unten sind meine Details.

Produktschema:

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); 
     }); 

ich diesen Code versucht, zu

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); 

Wo das Unrecht ist das?

Edit: MongoDB Daten:

{ 
    "_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

Ihre Abfrage sieht gut aus und es funktioniert für mich. Vielleicht können Sie einige Beispieldaten veröffentlichen, die Sie erwarten und die Sie bekommen. –

+0

@node_saini, Sicher und fertig – Charlie

+0

Im Schema ist Ihr Preisdatentyp Zahl, aber in Ihrem Datenpreis ist Zeichenfolge, so wird es nicht entsprechend arbeiten. [Verweise] (http://stackoverflow.com/q/18039358/4394926) und es gibt mehrere Beiträge zu diesem Thema, verweisen Sie sie und Sie erhalten Ihr erwartetes Ergebnis. –

Antwort

2

Ihre Anfrage korrekt ist das Problem ist, wenn Sie Preis in Ihrer Datenbank speichern Sie eine Zeichenfolge speichern einer Reihe und $ gt insted und lt auf ein Werk $ Nummer nicht auf Zeichenfolge

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 
Verwandte Themen