SQL 如何使用“like”查询 MongoDB?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3305561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:54:27  来源:igfitidea点击:

How to query MongoDB with "like"?

sqlmongodbmongodb-querysql-like

提问by Freewind

I want to query something with SQL's likequery:

我想用 SQL 的like查询来查询:

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

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

如何在 MongoDB 中实现相同的目标?我like文档中找不到操作符。

回答by Kyle H

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 Regexp's '.*'), not something that has "m" anchored to the beginning of the string.

您正在寻找某处包含“m”的东西(SQL 的“' %”运算符相当于 Regexp 的“ .*'”),而不是将“m”锚定到字符串开头的东西。

note:mongodb uses regular expressions which are more powerful than "LIKE" in sql. With regular expressions you can create any pattern that you imagine.

注意:mongodb 使用的正则表达式比 sql 中的“LIKE”更强大。使用正则表达式,您可以创建您想象的任何模式。

For more info on regular expressions refer to this link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

有关正则表达式的更多信息,请参阅此链接 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

回答by Johnathan Douglas

db.users.insert({name: 'paulo'})
db.users.insert({name: 'patric'})
db.users.insert({name: 'pedro'})

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

out: paulo, patric

出局:保罗、帕特里克

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

out: paulo, patric

出局:保罗、帕特里克

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

out: pedro

出局:佩德罗

回答by Afshin Mehrabani

In

  • PyMongousing Python
  • Mongooseusing Node.js
  • Jongo, using Java
  • mgo, using Go
  • 使用Python 的PyMongo
  • 猫鼬使用Node.js
  • Jongo, 使用Java
  • mgo,使用Go

you can do:

你可以做:

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

回答by leon

In PHP, you could use following code:

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

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

回答by Joshua Partogi

You would use regex for that in mongo.

你会在 mongo 中使用正则表达式。

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

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

回答by Somnath Muluk

There are already many answers. I am giving different types of requirements and solutions for string search with regex.

已经有很多答案了。我给出了使用正则表达式进行字符串搜索的不同类型的要求和解决方案。

You can do with regex which contain word i.e like. Also you can use $options => ifor case insensitive search

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

Contains string

包含 string

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

Doesn't Contains stringonly with regex

不只包含string正则表达式

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

Exact case insensitive string

不区分大小写 string

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

Start with string

从...开始 string

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

End with string

string

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

Keep thisas a bookmark, and a reference for any other alterations you may need.

保持为书签,以及您可能需要的任何其他改变的参考。

回答by user3645907

You have 2 choices:

您有 2 个选择:

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

or

或者

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

On 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 regular expression as you want.

在第二个选项中,您有更多选项,例如选项中的“i”以使用不区分大小写的方式查找。而关于“string”,你可以使用像“ .string.”(%string%),或者“string.*”(string%)和“.*string)(%string)”例如。你可以使用正则表达式如你所愿。

Enjoy!

享受!

回答by Eddy

If using node.js, itsays that you can write this:

如果使用node.js说你可以这样写:

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') } );

回答by The6thSense

Already u got the answers but to match regex with case insensitivity

你已经得到了答案,但要匹配不区分大小写的正则表达式

You could use the following query

您可以使用以下查询

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

The iin the /m/iindicates case insensitivity and .pretty()provides a more pretty output

i/m/i表示不区分大小写和.pretty()提供了一个更漂亮输出

回答by Aqib Mumtaz

For Mongoose in Node.js

对于 Node.js 中的猫鼬

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