Yanıtlar:
Aşağıdaki kodla yapabilirsiniz.
db.users.findOne({"username" : {$regex : ".*son.*"}});
db.users.findOne({"username" : {$regex : "son"}});
Mongo kabuğu regex'i desteklediğinden, bu tamamen mümkündür.
db.users.findOne({"username" : /.*son.*/});
Sorgunun büyük / küçük harfe duyarsız olmasını istiyorsak, aşağıda gösterildiği gibi "i" seçeneğini kullanabiliriz:
db.users.findOne({"username" : /.*son.*/i});
Bkz. Http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions
db.users.findOne({"username" : /.*son.*/});de aşırı olabilir ve normal regex olabilir/son/
{ username: /son/ }
https://docs.mongodb.com/manual/reference/sql-comparison/
http://php.net/manual/en/mongo.sqltomongo.php
MySQL
SELECT * FROM users WHERE username LIKE "%Son%"
MongoDB
db.users.find({username:/Son/})
Sürüm 2.4'ten itibaren, sorgulamak için $ text operatörünü aramak ve kullanmak için alan (lar) üzerinde bir metin dizini oluşturabilirsiniz .
İlk olarak, dizini oluşturun:
db.users.createIndex( { "username": "text" } )
Ardından, aramak için:
db.users.find( { $text: { $search: "son" } } )
Deneyler (~ 150K belgeler):
Notlar:
db.collection.createIndex( { "$**": "text" } ).Bu arama motorlarındaki ilk hitlerden biri olduğu ve yukarıdakilerin hiçbiri MongoDB 3.x için işe yaramadığı için, işleyen bir regex araması:
db.users.find( { 'name' : { '$regex' : yourvalue, '$options' : 'i' } } )
Gerek yok ve ekstra dizin veya benzer.
MongoDB'yi Python üzerinden bağlarsanız yapmanız gerekenler
db.users.find({"username": {'$regex' : '.*' + 'Son' + '.*'}})
'Son' yerine bir değişken adı ve dolayısıyla dize birleşimi de kullanabilirsiniz.
.*${value}.*}
Bu görevi yerine getirmenin en basit yolu
Sorgunun büyük / küçük harfe duyarlı olmasını istiyorsanız
db.getCollection("users").find({'username':/Son/})
Sorgunun büyük / küçük harfe duyarsız olmasını istiyorsanız
db.getCollection("users").find({'username':/Son/i})
Bu işi yapmalı
db.users.find({ username: { $in: [ /son/i ] } });
iHarflerin tek vaka eşleşen kısıtlamaları önlemek için sadece var.
MongoDB belgelerindeki $ regex belgelerine bakabilirsiniz. İşte bir bağlantı: https://docs.mongodb.com/manual/reference/operator/query/regex/
RegExp eşleşmesinde HTML etiketlerini yoksayma:
var text = '<p>The <b>tiger</b> (<i>Panthera tigris</i>) is the largest <a href="https://stackoverflow.com/wiki/Felidae" title="Felidae">cat</a> <a href="https://stackoverflow.com/wiki/Species" title="Species">species</a>, most recognizable for its pattern of dark vertical stripes on reddish-orange fur with a lighter underside. The species is classified in the genus <i><a href="https://stackoverflow.com/wiki/Panthera" title="Panthera">Panthera</a></i> with the <a href="https://stackoverflow.com/wiki/Lion" title="Lion">lion</a>, <a href="https://stackoverflow.com/wiki/Leopard" title="Leopard">leopard</a>, <a href="https://stackoverflow.com/wiki/Jaguar" title="Jaguar">jaguar</a>, and <a href="https://stackoverflow.com/wiki/Snow_leopard" title="Snow leopard">snow leopard</a>. It is an <a href="https://stackoverflow.com/wiki/Apex_predator" title="Apex predator">apex predator</a>, primarily preying on <a href="https://stackoverflow.com/wiki/Ungulate" title="Ungulate">ungulates</a> such as <a href="https://stackoverflow.com/wiki/Deer" title="Deer">deer</a> and <a href="https://stackoverflow.com/wiki/Bovid" class="mw-redirect" title="Bovid">bovids</a>.</p>';
var searchString = 'largest cat species';
var rx = '';
searchString.split(' ').forEach(e => {
rx += '('+e+')((?:\\s*(?:<\/?\\w[^<>]*>)?\\s*)*)';
});
rx = new RegExp(rx, 'igm');
console.log(text.match(rx));
MongoDB birleştirme filtresine dönüştürmek muhtemelen çok kolaydır.