回答:
次のコードでそれを行うことができます。
db.users.findOne({"username" : {$regex : ".*son.*"}});
db.users.findOne({"username" : {$regex : "son"}});
Mongoシェルは正規表現をサポートしているため、それは完全に可能です。
db.users.findOne({"username" : /.*son.*/});
クエリで大文字と小文字を区別しないようにする場合は、次のように「i」オプションを使用できます。
db.users.findOne({"username" : /.*son.*/i});
参照:http : //www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions
db.users.findOne({"username" : /.*son.*/});
やり過ぎかもしれませんし、正規表現も簡単かもしれません/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/})
バージョン2.4以降では、フィールドにテキストインデックスを作成して検索し、クエリに$ text演算子を使用できます。
まず、インデックスを作成します。
db.users.createIndex( { "username": "text" } )
次に、検索するには:
db.users.find( { $text: { $search: "son" } } )
ベンチマーク(〜15万文書):
ノート:
db.collection.createIndex( { "$**": "text" } )
。Pythonを介してMongoDBに接続する場合は、次のことを行う必要があります
db.users.find({"username": {'$regex' : '.*' + 'Son' + '.*'}})
'Son'の代わりに変数名を使用して、文字列を連結することもできます。
.*${value}.*
}を使用できます
これでうまくいくはずです
db.users.find({ username: { $in: [ /son/i ] } });
これi
は、1つの文字の大文字と小文字を照合する制限を防ぐためにあります。
MongoDBのドキュメントで$ regexのドキュメントを確認できます。ここにリンクがあります:https : //docs.mongodb.com/manual/reference/operator/query/regex/
RegExp一致でHTMLタグを無視する方法:
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集約フィルターに変えることができます。