简体   繁体   English

在 mongoose 的 findOne 条件下操作数据

[英]Manipulate data inside conditions of findOne in mongoose

I want to convert title in also lower case without considering space inside findOne conditions so that i can compare it with requested title.我想将标题也转换为小写而不考虑findOne条件中的空间,以便我可以将它与请求的标题进行比较。 New Post => newpost want to convert like this: New Post => newpost 想像这样转换:

 app.get("/posts/:postName", function(req, res){
      const requestedTitle = _.lowerCase(req.params.postName);
      
      Post.findOne({**title : requestedTitle**}, function(err, post){
        if (!err) {
            res.render("post", {
              title: post.title,
              content: post.body
            });
          }
        });
    });

You can use .replace(/ /g, '') to delete all space, then you can use .toLowerCase() to lowercase the title.您可以使用.replace(/ /g, '')删除所有空格,然后您可以使用.toLowerCase()将标题小写。

Example code:示例代码:

const requestedTitle = req.params.postName.replace(/ /g, '').toLowerCase();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM