使用nodejs通过名称从集合中检索对象

所以我目前正在使用MEAN堆栈进行应用程序。 我现在遇到的问题是,当打电话到API,我能够成功检索所有对象和每个对象从数据库中的ID(使用POSTMAN(铬))我已经设置使用mongoose&express路由器。 我的问题是,我可以通过它的名字检索一个对象吗? 我一直在网上search,我不确定如何实现这一点。 例如:这是我目前的Api代码。

var Dishes = require('../../app/models/dishes'); var Terms = require('../../app/models/terms'); var config = require('../../config'); module.exports = function(app,express){ // api --------------------------------------------------------------------- var apiRouter = express.Router(); // middleware to use for all requests apiRouter.use(function (req, res, next) { // do logging console.log('Somebody just came to our app!'); next(); }); // Test routes to make sure everything is working //(accessed at GET http://localhost:3000/api) apiRouter.get('/', function (req, res) { res.json({message: 'Welcome to the API!'}); }); /** ================================= Dishes ==========================================**/ //on routes that end in /dishes , show all dishes in json apiRouter.get('/dishes', function (req, res) { Dishes.find(function (err, dishes) { // if there is an error retrieving, send the error. nothing after res.send(err) will execute if (err) res.send(err); res.json(dishes); // return all dishes in JSON format }); }); //on routes that end in /dishes/:_id , show all the this with the corresponding ID // get the dish with that id // (accessed at GET http://localhost:8080/api/dishes/:dish_id) apiRouter.get('/dishes/:_id',function(req, res) { Dishes.findById(req.params._id, function(err, dish) { if (err) res.send(err); // return that dish res.json(dish); }); }); return apiRouter; }; 

我访问的菜式如下:

  var mongoose = require('mongoose'); var Schema = mongoose.Schema; //with Mongoose everything is derived from a schema ! Lets get a reference and define our Dishes Schema var DishSchema = mongoose.Schema({ dishName: {type: String, index: {unique: true}}, Desc : {type: String, index: { unique: true}}, Allergy: String, HealthRisks: String },{collection:'Dishes'}); module.exports = DishSchema; //The Next step is to compile our schema into a model var Dishes = mongoose.model('Dishes', DishSchema);//Dish Schema into model // return the model module.exports = mongoose.model('Dishes', DishSchema) 

我想要做的是做一个API调用(/盘/盘名),并返回相关的菜。 任何帮助将不胜感激。

 apiRouter.get('/dishes/getByName/:dishName',function(req, res) { Dishes.findOne({dishName:req.params.dishName}, function(err, dish) { if (err) res.send(err); // return that dish res.send(dish); }); });