5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MongoDBで変数でlike検索の実行

Last updated at Posted at 2017-05-14

findメソッドで検索対象に変数を使う方法についてつまづいたのでメモ。

単純にやりがちだがこれではエラー。
"/"と/が異なるので。

var word = "テスト"
var query = ({test: "/" + word + "/"})
testDB.find(query).function(err, item) {
 //hogehoge
}

RegExpを使うことで解決

var word = "テスト"
var query = ({test: new RegExp(".*" + word + ".*" , "i")})
testDB.find(query).function(err, item) {
 //hogehoge
}
5
2
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?