【问题标题】:How to find items using regex in Mongoose [duplicate]如何在 Mongoose 中使用正则表达式查找项目 [重复]
【发布时间】:2016-11-24 15:47:03
【问题描述】:

在 Mongoose 文档中,我没有找到 MongoDb 的 $regex 的等价物。你能提供一个带有正则表达式的简单 Mongoose find() 吗?

【问题讨论】:

标签: regex mongodb mongoose


【解决方案1】:

猫鼬doc for find.

mongodbdoc for regex.

   var Person = mongoose.model('Person', yourSchema);
   // find each person with a name contains 'Ghost'
   Person.findOne({ "name" : { $regex: /Ghost/, $options: 'i' } },
          function (err, person) {
                 if (err) return handleError(err);
                 console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation);

   });

注意我们传递给 mongoose.findOne 函数的第一个参数。 "{ "name" : { $regex: /Ghost/, $options: 'i' } }"。 “名称”是您正在搜索的文档的字段。 “鬼”是正则表达式。 "i" 用于不区分大小写的匹配。希望这会对你有所帮助。

【讨论】:

  • 它有帮助。谢谢,@ShashithDarshana!
  • 谢谢 - 这很有帮助 - 只有一件事......如果你有一个变量,你似乎不需要包含“/”。只是 {$regex:myVar, $options:'I'}}。在变量名之前添加“/”或将它们连接起来对我不起作用。
猜你喜欢
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 2015-03-20
  • 2012-06-17
  • 1970-01-01
相关资源
最近更新 更多