如何使用“like”查询MongoDB

发布于 2024-09-11 01:53:25 字数 262 浏览 12 评论 0 原文

我想使用 SQL 的 like 查询来查询某些内容:

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

如何在 MongoDB 中实现相同的目的?我在文档like的运算符一个>。

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

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

How can I achieve the same in MongoDB? I can't find an operator for like in the documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

ま柒月 2024-09-18 01:53:26

您有两种选择:

db.users.find({"name": /string/})

db.users.find({"name": {"$regex": "string", "$options": "i"}})

对于第二个选择,您有更多选项,例如选项中的“i”来查找不区分大小写的情况。

关于“字符串”,您可以使用“.string.”(%string%)或“string.*”(string%)和“.*string)(%string)”您可以根据需要使用正则表达式。

You have two choices:

db.users.find({"name": /string/})

or

db.users.find({"name": {"$regex": "string", "$options": "i"}})

For the second one, you have more options, like "i" in options to find using case insensitive.

And about the "string", you can use like ".string." (%string%), or "string.*" (string%) and ".*string) (%string) for example. You can use a regular expression as you want.

优雅的叶子 2024-09-18 01:53:26

如果使用 Node.js它会显示 你可以这样写:

db.collection.find( { field: /acme.*corp/i } );

// Or
db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } );

另外,你可以这样写:

db.collection.find( { field: new RegExp('acme.*corp', 'i') } );

If using Node.js, it says that you can write this:

db.collection.find( { field: /acme.*corp/i } );

// Or
db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } );

Also, you can write this:

db.collection.find( { field: new RegExp('acme.*corp', 'i') } );
还在原地等你 2024-09-18 01:53:26

您已经得到了答案,但要与不区分大小写的正则表达式匹配,您可以使用以下查询:

db.users.find ({ "name" : /m/i } ).pretty()

/m/i 中的 i 表示不区分大小写, .pretty() 提供更漂亮的输出。

Already you got the answers, but to match with a regular expression with case insensitivity, you could use the following query:

db.users.find ({ "name" : /m/i } ).pretty()

The i in the /m/i indicates case insensitivity and .pretty() provides a prettier output.

も星光 2024-09-18 01:53:26

对于 Node.js 中的 Mongoose

db.users.find({'name': {'$regex': '.*sometext.*'}})

For Mongoose in Node.js:

db.users.find({'name': {'$regex': '.*sometext.*'}})
云胡 2024-09-18 01:53:26

在 MongoDb 中,可以像使用 MongoDb 引用运算符正则表达式(regex)

对于同一个前任。

MySQL - SELECT * FROM users  WHERE name LIKE '%m%'

MongoDb

    1) db.users.find({ "name": { "$regex": "m", "$options": "i" } })

    2) db.users.find({ "name": { $regex: new RegExp("m", 'i') } })

    3) db.users.find({ "name": { $regex:/m/i } })

    4) db.users.find({ "name": /mail/ })

    5) db.users.find({ "name": /.*m.*/ })

MySQL - SELECT * FROM users  WHERE name LIKE 'm%'

MongoDb Any of Above with /^String/

    6) db.users.find({ "name": /^m/ })

MySQL - SELECT * FROM users  WHERE name LIKE '%m'

MongoDb Any of Above with /String$/

    7) db.users.find({ "name": /m$/ })

In MongoDb, can use like using MongoDb reference operator regular expression(regex).

For Same Ex.

MySQL - SELECT * FROM users  WHERE name LIKE '%m%'

MongoDb

    1) db.users.find({ "name": { "$regex": "m", "$options": "i" } })

    2) db.users.find({ "name": { $regex: new RegExp("m", 'i') } })

    3) db.users.find({ "name": { $regex:/m/i } })

    4) db.users.find({ "name": /mail/ })

    5) db.users.find({ "name": /.*m.*/ })

MySQL - SELECT * FROM users  WHERE name LIKE 'm%'

MongoDb Any of Above with /^String/

    6) db.users.find({ "name": /^m/ })

MySQL - SELECT * FROM users  WHERE name LIKE '%m'

MongoDb Any of Above with /String$/

    7) db.users.find({ "name": /m$/ })
独﹏钓一江月 2024-09-18 01:53:26

使用 MongoDB Compass,您需要使用严格模式语法,如下所示:(

{ "text": { "$regex": "^Foo.*", "$options": "i" } }

在 MongoDB Compass 中,使用 " 而不是 ' 很重要)

With MongoDB Compass, you need to use the strict mode syntax, as such:

{ "text": { "$regex": "^Foo.*", "$options": "i" } }

(In MongoDB Compass, it's important that you use " instead of ')

撑一把青伞 2024-09-18 01:53:26

您可以使用MongoDB 2.6的新功能:

db.foo.insert({desc: "This is a string with text"});
db.foo.insert({desc:"This is a another string with Text"});
db.foo.ensureIndex({"desc":"text"});
db.foo.find({
    $text:{
        $search:"text"
    }
});

You can use the new feature of MongoDB 2.6:

db.foo.insert({desc: "This is a string with text"});
db.foo.insert({desc:"This is a another string with Text"});
db.foo.ensureIndex({"desc":"text"});
db.foo.find({
    $text:{
        $search:"text"
    }
});
浪推晚风 2024-09-18 01:53:26

Node.js 项目中并使用 Mongoose ,使用like查询:

var User = mongoose.model('User');

var searchQuery = {};
searchQuery.email = req.query.email;
searchQuery.name = {$regex: req.query.name, $options: 'i'};
User.find(searchQuery, function(error, user) {
                if(error || user === null) {
                    return res.status(500).send(error);
                }
                return res.status(200).send(user);
            });

In a Node.js project and using Mongoose, use a like query:

var User = mongoose.model('User');

var searchQuery = {};
searchQuery.email = req.query.email;
searchQuery.name = {$regex: req.query.name, $options: 'i'};
User.find(searchQuery, function(error, user) {
                if(error || user === null) {
                    return res.status(500).send(error);
                }
                return res.status(200).send(user);
            });
冷默言语 2024-09-18 01:53:26

您可以使用 where 语句构建任何 JavaScript 脚本:

db.myCollection.find( { $where: "this.name.toLowerCase().indexOf('m') >= 0" } );

参考:$where

You can use a where statement to build any JavaScript script:

db.myCollection.find( { $where: "this.name.toLowerCase().indexOf('m') >= 0" } );

Reference: $where

风情万种。 2024-09-18 01:53:26

字符串yourdb = {deepakparmar,dipak,parmar}

db.getCollection('yourdb').find({"name":/^dee/})

ans deepakparmar

db.getCollection('yourdb').find({"name":/d/})

ans deepakparmar,dipak

db.getCollection('yourdb').find({"name":/mar$/})

ans deepakparmar,parmar

String yourdb={deepakparmar, dipak, parmar}

db.getCollection('yourdb').find({"name":/^dee/})

ans deepakparmar

db.getCollection('yourdb').find({"name":/d/})

ans deepakparmar, dipak

db.getCollection('yourdb').find({"name":/mar$/})

ans deepakparmar, parmar

君勿笑 2024-09-18 01:53:26

在 Go 和 mgo 驱动程序中:

Collection.Find(bson.M{"name": bson.RegEx{"m", ""}}).All(&result)

结果是所需类型的结构体实例。

In Go and the mgo driver:

Collection.Find(bson.M{"name": bson.RegEx{"m", ""}}).All(&result)

where the result is the struct instance of the sought-after type.

软糖 2024-09-18 01:53:26

在 SQL 中,“like”查询如下所示:

select * from users where name like '%m%'

在 MongoDB 控制台中,它如下所示:

db.users.find({"name": /m/})     // Not JSON formatted

db.users.find({"name": /m/}).pretty()  // JSON formatted

另外,pretty() 方法将生成格式化的 JSON 结构在所有更具可读性的地方。

In SQL, the ‘like’ query looks like this:

select * from users where name like '%m%'

In the MongoDB console, it looks like this:

db.users.find({"name": /m/})     // Not JSON formatted

db.users.find({"name": /m/}).pretty()  // JSON formatted

In addition, the pretty() method will produce a formatted JSON structure in all the places which is more readable.

北风几吹夏 2024-09-18 01:53:26

对于 PHP mongo就像

我在使用 PHP mongo 时遇到了一些问题,比如。我发现连接正则表达式参数在某些情况下会有所帮助 - PHP mongo find field以开头。

例如,

db()->users->insert(['name' => 'john']);
db()->users->insert(['name' => 'joe']);
db()->users->insert(['name' => 'jason']);

// starts with
$like_var = 'jo';
$prefix = '/^';
$suffix = '/';
$name = $prefix . $like_var . $suffix;
db()->users->find(['name' => array('$regex'=>new MongoRegex($name))]);
output: (joe, john)

// contains
$like_var = 'j';
$prefix = '/';
$suffix = '/';
$name = $prefix . $like_var . $suffix;
db()->users->find(['name' => array('$regex'=>new MongoRegex($name))]);

output: (joe, john, jason)

For PHP mongo Like.

I had several issues with PHP mongo like. I found that concatenating the regular expression parameters helps in some situations - PHP mongo find field starts with.

For example,

db()->users->insert(['name' => 'john']);
db()->users->insert(['name' => 'joe']);
db()->users->insert(['name' => 'jason']);

// starts with
$like_var = 'jo';
$prefix = '/^';
$suffix = '/';
$name = $prefix . $like_var . $suffix;
db()->users->find(['name' => array('$regex'=>new MongoRegex($name))]);
output: (joe, john)

// contains
$like_var = 'j';
$prefix = '/';
$suffix = '/';
$name = $prefix . $like_var . $suffix;
db()->users->find(['name' => array('$regex'=>new MongoRegex($name))]);

output: (joe, john, jason)
み零 2024-09-18 01:53:26

将模板文字与变量一起使用也可以:

{"firstname": {$regex : `^${req.body.firstname}.*` , $options: 'si' }}

Using template literals with variables also works:

{"firstname": {$regex : `^${req.body.firstname}.*` , $options: 'si' }}

假情假意假温柔 2024-09-18 01:53:26

正则表达式的处理成本很高。

另一种方法是创建文本索引,然后使用 $search 进行搜索。

创建要搜索的字段的文本索引

db.collection.createIndex({name: 'text', otherField: 'text'});

在文本索引中搜索字符串:

db.collection.find({
  '$text'=>{'$search': "The string"}
})

Regular expressions are expensive to process.

Another way is to create an index of text and then search it using $search.

Create a text index of fields you want to make searchable:

db.collection.createIndex({name: 'text', otherField: 'text'});

Search for a string in the text index:

db.collection.find({
  '$text'=>{'$search': "The string"}
})
别想她 2024-09-18 01:53:26

使用正则表达式匹配如下。 “i”表示不区分大小写。

var collections = mongoDatabase.GetCollection("Abcd");

var queryA = Query.And(
         Query.Matches("strName", new BsonRegularExpression("ABCD", "i")), 
         Query.Matches("strVal", new BsonRegularExpression("4121", "i")));

var queryB = Query.Or(
       Query.Matches("strName", new BsonRegularExpression("ABCD","i")),
       Query.Matches("strVal", new BsonRegularExpression("33156", "i")));

var getA = collections.Find(queryA);
var getB = collections.Find(queryB);

Use regular expressions matching as below. The 'i' shows case insensitivity.

var collections = mongoDatabase.GetCollection("Abcd");

var queryA = Query.And(
         Query.Matches("strName", new BsonRegularExpression("ABCD", "i")), 
         Query.Matches("strVal", new BsonRegularExpression("4121", "i")));

var queryB = Query.Or(
       Query.Matches("strName", new BsonRegularExpression("ABCD","i")),
       Query.Matches("strVal", new BsonRegularExpression("33156", "i")));

var getA = collections.Find(queryA);
var getB = collections.Find(queryB);
青衫负雪 2024-09-18 01:53:26

似乎同时使用 JavaScript /regex_pattern/ 模式和 MongoDB {'$regex': 'regex_pattern'} 模式都是有原因的。请参阅: MongoDB RegEx 语法限制

这不是一个完整的正则表达式教程,但在看到上面的高票模棱两可的帖子后,我受到了运行这些测试的启发。

> ['abbbb','bbabb','bbbba'].forEach(function(v){db.test_collection.insert({val: v})})

> db.test_collection.find({val: /a/})
{ "val" : "abbbb" }
{ "val" : "bbabb" }
{ "val" : "bbbba" }

> db.test_collection.find({val: /.*a.*/})
{ "val" : "abbbb" }
{ "val" : "bbabb" }
{ "val" : "bbbba" }

> db.test_collection.find({val: /.+a.+/})
{ "val" : "bbabb" }

> db.test_collection.find({val: /^a/})
{ "val" : "abbbb" }

> db.test_collection.find({val: /a$/})
{ "val" : "bbbba" }

> db.test_collection.find({val: {'$regex': 'a
}})
{ "val" : "bbbba" }

It seems that there are reasons for using both the JavaScript /regex_pattern/ pattern as well as the MongoDB {'$regex': 'regex_pattern'} pattern. See: MongoDB RegEx Syntax Restrictions

This is not a complete regular expression tutorial, but I was inspired to run these tests after seeing a highly voted ambiguous post above.

> ['abbbb','bbabb','bbbba'].forEach(function(v){db.test_collection.insert({val: v})})

> db.test_collection.find({val: /a/})
{ "val" : "abbbb" }
{ "val" : "bbabb" }
{ "val" : "bbbba" }

> db.test_collection.find({val: /.*a.*/})
{ "val" : "abbbb" }
{ "val" : "bbabb" }
{ "val" : "bbbba" }

> db.test_collection.find({val: /.+a.+/})
{ "val" : "bbabb" }

> db.test_collection.find({val: /^a/})
{ "val" : "abbbb" }

> db.test_collection.find({val: /a$/})
{ "val" : "bbbba" }

> db.test_collection.find({val: {'$regex': 'a
}})
{ "val" : "bbbba" }
遗失的美好 2024-09-18 01:53:26

like 查询如下所示:

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

对于 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]

A like query would be as shown below:

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

For the 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]
度的依靠╰つ 2024-09-18 01:53:26

如果你使用Spring-Data MongoDB,你可以这样做:

String tagName = "m";
Query query = new Query();
query.limit(10);
query.addCriteria(Criteria.where("tagName").regex(tagName));

If you are using Spring-Data MongoDB, you can do it in this way:

String tagName = "m";
Query query = new Query();
query.limit(10);
query.addCriteria(Criteria.where("tagName").regex(tagName));
护你周全 2024-09-18 01:53:26

如果您有一个字符串变量,则必须将其转换为正则表达式,因此 MongoDB 将对其使用 like 语句。

const name = req.query.title; //John
db.users.find({ "name": new Regex(name) });

与以下结果相同:

db.users.find({"name": /John/})

If you have a string variable, you must convert it to a regex, so MongoDB will use a like statement on it.

const name = req.query.title; //John
db.users.find({ "name": new Regex(name) });

Is the same result as:

db.users.find({"name": /John/})
戏舞 2024-09-18 01:53:26

查找结果的一种方法与类似查询相同:

db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})

其中i用于不区分大小写的获取数据。

另一种方法是我们还可以得到结果:

db.collection.find({"name":/aus/})

上面将提供包含 aus 的名称中包含 aus 的结果。

One way to find the result as with equivalent to a like query:

db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})

Where i is used for a case-insensitive fetch data.

Another way by which we can also get the result:

db.collection.find({"name":/aus/})

The above will provide the result which has the aus in the name containing aus.

青丝拂面 2024-09-18 01:53:26

使用聚合子字符串搜索(带索引!!!):

db.collection.aggregate([{
        $project : {
            fieldExists : {
                $indexOfBytes : ['$field', 'string']
            }
        }
    }, {
        $match : {
            fieldExists : {
                $gt : -1
            }
        }
    }, {
        $limit : 5
    }
]);

Use aggregation substring search (with index!!!):

db.collection.aggregate([{
        $project : {
            fieldExists : {
                $indexOfBytes : ['$field', 'string']
            }
        }
    }, {
        $match : {
            fieldExists : {
                $gt : -1
            }
        }
    }, {
        $limit : 5
    }
]);
风吹短裙飘 2024-09-18 01:53:26

您可以使用正则表达式进行查询:

db.users.find({"name": /m/});

如果字符串来自用户,也许您想在使用字符串之前对其进行转义。这将防止用户的文字字符被解释为正则表达式标记。

例如,搜索字符串“A”。如果不转义,也将匹配“AB”。
您可以在使用字符串之前使用简单的replace 对其进行转义。我将其设置为可重用的函数:

function textLike(str) {
  var escaped = str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\
amp;');
  return new RegExp(escaped, 'i');
}

所以现在,字符串变成了不区分大小写的模式,也与文字点匹配。示例:

>  textLike('A.');
<  /A\./i

现在我们准备好随时生成正则表达式:

db.users.find({ "name": textLike("m") });

You can query with a regular expression:

db.users.find({"name": /m/});

If the string is coming from the user, maybe you want to escape the string before using it. This will prevent literal chars from the user to be interpreted as regex tokens.

For example, searching the string "A." will also match "AB" if not escaped.
You can use a simple replace to escape your string before using it. I made it a function for reusing:

function textLike(str) {
  var escaped = str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\
amp;');
  return new RegExp(escaped, 'i');
}

So now, the string becomes a case-insensitive pattern matching also the literal dot. Example:

>  textLike('A.');
<  /A\./i

Now we are ready to generate the regular expression on the go:

db.users.find({ "name": textLike("m") });
我的痛♀有谁懂 2024-09-18 01:53:26

如果您想在 MongoDB 中进行“类似”搜索,那么您应该使用 $regex。通过使用它,查询将是:

db.product.find({name:{$regex:/m/i}})

有关更多信息,您也可以阅读文档 - $正则表达式

If you want a 'like' search in MongoDB then you should go with $regex. By using it, the query will be:

db.product.find({name:{$regex:/m/i}})

For more, you can read the documentation as well - $regex

落叶缤纷 2024-09-18 01:53:25

那必须是:

db.users.find({"name": /.*m.*/})

或者,类似:

db.users.find({"name": /m/})

您正在寻找某个地方包含“m”的内容(SQL 的 '%' 运算符相当于正则表达式' '.*'),而不是将“m”锚定到字符串开头的内容。

注意: MongoDB 使用正则表达式(参见文档) 比 SQL 中的“LIKE”更强大。使用正则表达式,您可以创建您想象的任何模式。

有关正则表达式的更多信息,请参阅正则表达式< /a>(MDN)。

That would have to be:

db.users.find({"name": /.*m.*/})

Or, similar:

db.users.find({"name": /m/})

You're looking for something that contains "m" somewhere (SQL's '%' operator is equivalent to regular expressions' '.*'), not something that has "m" anchored to the beginning of the string.

Note: MongoDB uses regular expressions (see docs) which are more powerful than "LIKE" in SQL. With regular expressions you can create any pattern that you imagine.

For more information on regular expressions, refer to Regular expressions (MDN).

离线来电— 2024-09-18 01:53:25
db.users.insert({name: 'patrick'})
db.users.insert({name: 'petra'})
db.users.insert({name: 'pedro'})

因此:

对于:

db.users.find({name: /a/})  // Like '%a%'

输出:patrick, petra

对于:

db.users.find({name: /^pa/}) // Like 'pa%'

输出:patrick

对于:

db.users.find({name: /ro$/}) // Like '%ro'

输出:pedro

db.users.insert({name: 'patrick'})
db.users.insert({name: 'petra'})
db.users.insert({name: 'pedro'})

Therefore:

For:

db.users.find({name: /a/})  // Like '%a%'

Output: patrick, petra

For:

db.users.find({name: /^pa/}) // Like 'pa%'

Output: patrick

For:

db.users.find({name: /ro$/}) // Like '%ro'

Output: pedro

深府石板幽径 2024-09-18 01:53:25

  • PyMongo 中,使用 Python
  • Mongoose 使用 Node.js
  • Jongo,使用 Java
  • mgo,使用Go

你可以这样做:

db.users.find({'name': {'$regex': 'sometext'}})

In

  • PyMongo using Python
  • Mongoose using Node.js
  • Jongo, using Java
  • mgo, using Go

you can do:

db.users.find({'name': {'$regex': 'sometext'}})
浸婚纱 2024-09-18 01:53:25

以下是使用正则表达式进行字符串搜索的不同类型的要求和解决方案。

您可以使用包含单词的正则表达式,即like。您也可以使用 $options =>; i 用于不区分大小写的搜索。

包含 string

db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})

不包含 string,仅包含正则表达式

db.collection.find({name:{'$regex' : '^((?!string).)*

不区分大小写 stringstring

db.collection.find({name:{'$regex' : '^string

开头 以 string

db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})

结尾string

db.collection.find({name:{'$regex' : 'string

保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。

, '$options' : 'i'}})

不区分大小写 stringstring


开头 以 string


结尾string


保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。

, '$options' : 'i'}})

开头 以 string


结尾string


保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。

, '$options' : 'i'}})

不区分大小写 stringstring


开头 以 string


结尾string


保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。

, '$options' : 'i'}})

保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。

, '$options' : 'i'}})

不区分大小写 stringstring


开头 以 string


结尾string


保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。

, '$options' : 'i'}})

开头 以 string


结尾string


保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。

, '$options' : 'i'}})

不区分大小写 stringstring


开头 以 string


结尾string


保留正则表达式备忘单 作为书签,以及您可能需要的任何其他更改的参考。

Here are different types of requirements and solutions for string search with regular expressions.

You can do with a regular expression which contains a word, i.e., like. Also you can use $options => i for a case insensitive search.

Contains string

db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})

Doesn't contain string, only with a regular expression

db.collection.find({name:{'$regex' : '^((?!string).)*

Exact case insensitive string

db.collection.find({name:{'$regex' : '^string

Start with string

db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})

End with string

db.collection.find({name:{'$regex' : 'string

Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.

, '$options' : 'i'}})

Exact case insensitive string


Start with string


End with string


Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.

, '$options' : 'i'}})

Start with string


End with string


Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.

, '$options' : 'i'}})

Exact case insensitive string


Start with string


End with string


Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.

, '$options' : 'i'}})

Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.

, '$options' : 'i'}})

Exact case insensitive string


Start with string


End with string


Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.

, '$options' : 'i'}})

Start with string


End with string


Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.

, '$options' : 'i'}})

Exact case insensitive string


Start with string


End with string


Keep Regular Expressions Cheat Sheet as a bookmark, and a reference for any other alterations you may need.

云归处 2024-09-18 01:53:25

在 PHP 中,您可以使用以下代码:

$collection->find(array('name'=> array('$regex' => 'm'));

In PHP, you could use the following code:

$collection->find(array('name'=> array('$regex' => 'm'));
梦过后 2024-09-18 01:53:25

您可以在 MongoDB 中使用正则表达式。

例如,

db.users.find({"name": /^m/})

You would use a regular expression for that in MongoDB.

For example,

db.users.find({"name": /^m/})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文