按标题searchMongoDB不是id

我试图让我的路线从博客应用程序的URL拉标题。 我得到它的工作使用id但无法弄清楚标题。 我知道我不能做findById() ,而是使用find() 。 我如何定制我的查询来查找标题。

路线

  app.get("/blogs/:title", function(req, res) { Blog.find(req.params.title, function(err, foundBlog){ if(err){ res.redirect("/blogs"); } else { res.render("show", {blog: foundBlog}); } }); }); 

模式

 var blogSchema = new mongoose.Schema({ title: String, image: String, body: String, created: {type: Date, default: Date.now}, author: { id: { type: mongoose.Schema.Types.ObjectId, ref: "User" }, email: String } }); 

您需要将具有模式密钥的对象传递给find()方法:

  app.get("/blogs/:title", function(req, res) { Blog.find({ title: req.params.title }, function(err, foundBlog){ if(err){ res.redirect("/blogs"); } else { res.render("show", {blog: foundBlog}); } }); });