How to query MongoDB with “like”?

后端 未结 30 2142
予麋鹿
予麋鹿 2020-11-21 05:51

I want to query something with SQL\'s like query:

SELECT * FROM users  WHERE name LIKE \'%m%\'

How to do I achieve the same in

30条回答
  •  [愿得一人]
    2020-11-21 06:23

    Like Query would be as shown below

    db.movies.find({title: /.*Twelve Monkeys.*/}).sort({regularizedCorRelation : 1}).limit(10);
    

    for scala ReactiveMongo api,

    val query = BSONDocument("title" -> BSONRegex(".*"+name+".*", "")) //like
    val sortQ = BSONDocument("regularizedCorRelation" -> BSONInteger(1))
    val cursor = collection.find(query).sort(sortQ).options(QueryOpts().batchSize(10)).cursor[BSONDocument]
    

提交回复
热议问题