【发布时间】:2016-12-11 22:45:55
【问题描述】:
我不知道如何查询价格。我目前的尝试不起作用,我不确定您必须在本地主机中输入什么。
http://localhost:3000/priceSearch?
我已经实现了 - orderSearch.find({price:{$gt:400, $lt: 700}})
我的 mongodb 中的价格字段是数字而不是字符串
谢谢你:D
这是我的代码:
priceSearch.ejs
var express = require('express');
var router = express.Router();
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/WishList';
router.get('/', function (req, res) {
var price = req.query.price;
MongoClient.connect(url, function (err, db) {
if (err) {
console.log("Unable to connect to the server", err);
} else {
console.log("Connection established...");
var orderSearch = db.collection('orders');
// find document who satisify price
orderSearch.find({price:{$gt:400, $lt: 700}}).toArray(function (err, result) {
if (err) {
res.send(err);
} else if (result.length) {
res.render('priceSearch',
{
priceSearch: result,
title: 'Product price search',
}
);
} else {
res.send("No documents found");
}
db.close();
});
}
});
});
module.exports = router;
【问题讨论】:
标签: javascript node.js express ejs