这/猫鼬在这个猫鼬中意味着什么?

时间:2016-09-12 01:24:01

标签: node.js mongodb mongoose

我试图从mongoose文档中了解以下内容:

// With a JSON doc
Person.
  find({
    occupation: /host/,
    'name.last': 'Ghost',

上述查询中/ /表示什么?我从第二部分看到,我们可以使用引号查询文字 - 'Ghost'。如果我们想查询变量名称主机,我们可以做主机吗?什么是/ host /?

1 个答案:

答案 0 :(得分:2)

/host/表示它是正则表达式查询:https://docs.mongodb.com/manual/reference/operator/query/regex/

为了解释手册,有四种方法可以在MongoDB中描述正则表达式查询:

{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }
{ <field>: { $regex: /pattern/<options> } }
{ <field>: /pattern/<options> }

在您的示例中,查询/host/'Ghost'都会匹配文字Ghost/host/也会匹配hostedhostessco-host等)。有关此主题的更多说明,请参阅https://en.wikipedia.org/wiki/Regular_expression

但请注意,关于索引使用的正则表达式查询有一些警告:https://docs.mongodb.com/manual/reference/operator/query/regex/#index-use

相关问题