【发布时间】:2017-01-25 15:06:22
【问题描述】:
因为我是 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);
我的查询:
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>• Moisture transfer properties. <br />• 7% stretch.<br />• Reflective safety trim.<br />• 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® 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
}
【问题讨论】:
-
您的查询看起来不错,对我有用。也许您可以发布一些您期望的示例数据以及您正在获得的数据。
-
@node_saini,确定完成
-
在架构中,您的价格数据类型是数字,但在您的数据中价格是字符串,因此它不会相应地工作。 Refer this 并且有几个关于这个主题的帖子,参考它们,你会得到你想要的结果。
-
@node_saini,我将价格数据类型更改为字符串,但仍然无法正常工作
-
那是因为您不会查询像这样的数字的字符串。您必须更改您的查询。可以看看我在之前的 cmets 中分享的链接。
标签: node.js mongodb filter schema