Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

sql - 如何用“ like”查询MongoDB?(How to query MongoDB with “like”?)

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

(我想查询一些与SQL的like查询:)

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

How to do I achieve the same in MongoDB?

(我如何在MongoDB中实现相同目标?)

I can't find an operator for like in the documentation .

(我在文档中找不到like的运算符。)

  ask by Freewind translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(注意: mongodb使用的正则表达式比sql中的“ LIKE”更强大。)

With regular expressions you can create any pattern that you imagine.

(使用正则表达式,您可以创建您想像的任何模式。)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...