Earlier we looked at querying string valued fields using
the greater than and less than operators. Returning to
our consideration of queries matching string fields. There are
more complicated sorts of queries that you might want to
perform on strings than just equality and inequality. For
example, we often want to look for patterns in
strings. MongoDB supports querying for string patterns using the
regex operator. Regex is based on a regular expression library.
In particular, the Perl Compatible Regular Expressions library.
For more information, I'd encourage you to just Google
for this. So the regex operator allows us
to do regular expression queries in MongoDB. Let's look
at some examples. Now in this collection, city
documents, contain a motto field or at least some
of them do. So as an example of
doing regex queries in MongoDB, let's take a look
at some of the words these mottos contain. This
is a very simple regular expression. In fact, we
don't even need regex to do this one. So
if I do the query this way, I should match
only documents where friendship is the entire string of
the motto. And here we go, friendship. Okay, so now
let's begin to expand this regular expression. What I'm
going to do first is introduce a very simple change.
Now we're looking for all mottos that contain
the word friendship. And friendship can either be capitalized
or not. And so here we have four, where
before we have simply one document. Okay so, now
let's expand this a little bit more. And if
your regex skills are a little rusty, please see
the instructor notes for documentation and pointers to some
tutorials. So let's expand this to include another word.
Okay, so this regular expression will identify all documents
containing a motto with either the word friendship or the
word pride. And either word can be capitalized. Let's
take a look at the actual models themselves. And again,
I'm going to use a projection to make it
a little bit easier to see the mottos. Okay and
we can see that each one of these has
either the word pride or the word friendship in it.
And capitalization doesn't matter. Okay so, that's about all the farther I
want to go with regex queries in this example. The point here is
that MongoDB supports Perl Compatible Regular Expressions. So, you have a lot of
power in what types of queries you can do involving string value fields.
ألقينا نظرة سابقًا على الاستعلام عن الحقول المقيّمة بسلسلة باستخدام
عاملي التشغيل greater than (أكبر من) وless than (أصغر من). وبالرجوع إلى
ما يتعلق بالاستعلامات المطابقة لحقول سلسلة. توجد
أنواع أكثر تعقيدًا من الاستعلامات التي قد تريد
إجراءها على السلاسل بخلاف التساوي وعدم التساوي. على
سبيل المثال، عادةً ما نريد البحث عن أنماط في
السلاسل. تدعم MongoDB الاستعلام عن أنماط السلاسل باستخدام
.عامل التشغيل regex. يستند عامل التشغيل regex إلى مكتبة تعبيرات عادية
.وبشكل خاص، مكتبة تعبيرات Perl العادية المتوافقة
للحصول على المزيد من المعلومات، أحثك على البحث في Google
عن هذه المكتبة. إذًا، يتيح لنا عامل التشغيل regex
إجراء استعلامات باستخدام التعبيرات العادية في MongoDB. لنلقِ نظرة
على بعض الأمثلة. والآن في هذه المجموعة، تشتمل مستندات
city (المدينة) على حقل motto (الشعار) أو تشتمل عليه بعض هذه المستندات
على الأقل. وكمثال
لإجراء استعلامات regex في MongoDB، دعنا نلقِ نظرة على
بعض الكلمات التي تشتمل عليها هذه الشعارات. هذا تعبير
عادي بسيط جدًا. وفي حقيقة الأمر، لا نحتاج حتى إلى
،regex لإجراء هذا الاستعلام. ولذلك
إذا أجريت الاستعلام بهذه الطريقة، فيتعين مطابقة
المستندات التي تكون فيها friendship (الصداقة) سلسلة كاملة لـ
،motto (الشعار). وتظهر هنا friendship. حسنًا
لنبدأ الآن توسيع نطاق هذا التعبير العادي. سأعمد أولاً إلى
.إجراء تغيير بسيط جدًا
نحن الآن بصدد البحث عن جميع الشعارات التي تشتمل على
كلمة friendship. ويمكن أن تبدأ كلمة friendship بحرف كبير
أو بدونه. إذًا، ترد الكلمة هنا أربع مرات، وذلك
قبل أن يتقلص عدد المستندات إلى مستند واحد فقط. حسنًا، لنوسع الآن
نطاق هذا الحقل قليلاً. وإذا كانت
قدرتك على استخدام regex محدودة قليلاً، فيرجى مراجعة
ملاحظات المدرب للاطلاع على وثائق ومؤشرات لبعض
.البرامج التعليمية. لنوسع إذًا هذا الحقل لتضمين كلمة أخرى
حسنًا، سيحدد هذا التعبير العادي جميع المستندات التي
تشتمل على شعار مقترن إما بكلمة friendship أو بكلمة
pride. ويمكن أن تبدأ كلتاهما بحرف كبير. فلنلقِ نظرة
على الأنماط الفعلية نفسها. وأكرر أنني
سأستخدم إحدى طرق العرض لتسهيل
،الاطلاع على الشعارات. حسنًا
يمكننا ملاحظة أن كلاً من هذه الجمل
.يشتمل إما على كلمة pride أو كلمة friendship
بالإضافة إلى أن بدء الكلمة بحرف كبير لا يمثل أهمية. حسنًا، هذا كل ما أريد التطرق إليه فيما
يتعلق باستعلامات regex في هذا المثال. وتكمن الأهمية هنا في
أن MongoDB تدعم تعبيرات Perl العادية المتوافقة. لذلك، تتوفر لديك إمكانية كبيرة
.في تحديد أنواع الاستعلامات التي يمكنك إجراؤها متضمنة حقولاً بقيمة سلسلة
Anteriormente, examinamos os campos com valor de string usando
os operadores maior que e menor que. Retornando à
nossa consideração de consultas que correspondem a campos de string. Há tipos
de consultas mais complicados do que você possa desejar para
executar em strings além de igualdade e desigualdade. Por
exemplo, com frequência, queremos procurar padrões em
strings. O MongoDB suporta a consulta de padrões de strings usando o
operador regex. O Regex é baseado em uma biblioteca de expressões regulares.
Especificamente, na biblioteca Perl Compatible Regular Expressions.
Para obter mais informações, pesquise sobre isso no
Google. O operador regex permite
fazer consultas de expressão regular no MongoDB. Vamos ver
alguns exemplos. Agora, nesta coleção, documentos
de cidade, há um campo motto ou pelos menos alguns
deles contêm. Como um exemplo de
consultas regex no MongoDB, vamos examinar
algumas das palavras que esses mottos contêm. Esta
é uma expressão regular muito simples. Na verdade, nem
precisamos do regex para esta consulta. Portanto,
se fizermos a consulta dessa maneira, ela deve corresponder
apenas a documentos onde friendship é a string inteira de
motto. E aqui está, friendship. Ok, agora,
vamos começar a expandir esta expressão regular. O que vou
fazer primeiro é introduzir uma alteração muito simples.
Agora estamos procurando todos os mottos que contêm
a palavra friendship. E friendship pode estar com a primeira letra maiúscula
ou não. E aqui temos quatro, onde
antes tínhamos simplesmente um documento. Ok, agora,
vamos expandir isso um pouco mais. E se
sua habilidade em regex estiver um pouco enferrujada, consulte
as notas do instrutor para obter a documentação e ponteiros para alguns
tutoriais. Vamos expandir isso para incluir outra palavra.
Ok, esta expressão regular identificará todos os documentos
que contêm um motto com a palavra friendship ou a
palavra pride. E qualquer uma das palavras pode estar com a primeira letra maiúscula. Vamos,
vamos dar uma olhada nos modelos reais. E, novamente,
vou usar uma projeção para tornar a
visualização dos mottos um pouco mais fácil. Ok,
Podemos ver que cada um destes tem
a palavra pride ou a palavra friendship.
E letras maiúsculas não importam. Ok. Este o máximo onde
queremos chegar com consultas regex neste exemplo. O ponto aqui é
que o MongoDB suporta as Perl Compatible Regular Expressions. Portanto, você tem muito
poder nos tipos de consultas que pode fazer envolvendo campos com valor de string.
早些时候 我们查看了使用大于和小于运算符
查询字符串值字段
返回到我们查询匹配字符串字段的注意事项
你可能需要在字符串上执行比等式和不等式
更复杂的查询分类
例如 我们经常需要查询字符串中的模式
MongoDB 支持使用 regex 运算符查询字符串模式
Regex 基于正则表达式库
特别是 Perl 兼容正则表达式库
有关更多信息 我鼓励你使用 Google 进行搜索
regex 运算符允许我们在 MongoDB 内进行正则表达式查询
让我们查看几个示例
现在 在本集合中
城市文档包含一个座右铭字段 至少其中一些城市文档包含该字段
因此 作为在 MongoDB 中进行 regex 查询的一个示例
让我们查看这些座右铭
包含的一些用词
这是非常简单的正则表达式
事实上 我们甚至不需要 regex 来做到这一点
因此 如果我们采用这种方法进行查询
我应该只匹配友谊为座右铭整个字符串的文档
在这里我们开始 友谊
让我们开始扩展该正则表达式
我首先要做的是引入非常简单的变化
现在 我们查找包含友谊一词的所有座右铭
友谊可以是大写也可以非大写
因此 我们这里有四个文档
而之前我们只有一个文档
现在 让我们进一步扩展
如果你的正则表达式技巧有点生疏
请参阅讲师注释文档和一些教程指南
因此 让我们扩展以包括另一个词语
因此 该正则表达式将确定
包含座右铭带词语友谊或词语骄傲的所有文档
任何一个词语都可以大写
让我们查看实际车型本身
我们将再次使用投影
以便更容易看到座右铭
我们可以看到其中每个
都有词语自豪或词语友谊
大些并不重要 因此
这是我想要在本示例中进一步使用 regex 查询的所有内容
这里的重点在于 MongoDB 支持 Perl 兼容的正则表达式
所以 你在确定你可以对相关字符串值字段进行什么类型的查询方面有很大权力