MongoDB:正規表現入力に似た名前を見つけますか?
MongoDBで$regexを使用して名前を送信し、入力に類似した名前を見つけます。ドキュメントを使用してコレクションを作成しましょう-
> db.demo514.insertOne({"Information":{"FullName":"John Doe"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e885116987b6e0e9d18f58c")
}
> db.demo514.insertOne({"Information":{"FullName":"John Smith"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e88515e987b6e0e9d18f58d")
}
> db.demo514.insertOne({"Information":{"FullName":"john doe"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e885169987b6e0e9d18f58e")
}
> db.demo514.insertOne({"Information":{"FullName":"Chris Brown"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e88516f987b6e0e9d18f58f")
} find()メソッドを使用してコレクションからすべてのドキュメントを表示する-
> db.demo514.find();
これにより、次の出力が生成されます-
{ "_id" : ObjectId("5e885116987b6e0e9d18f58c"), "Information" : { "FullName" : "John Doe" } }
{ "_id" : ObjectId("5e88515e987b6e0e9d18f58d"), "Information" : { "FullName" : "John Smith" } }
{ "_id" : ObjectId("5e885169987b6e0e9d18f58e"), "Information" : { "FullName" : "john doe" } }
{ "_id" : ObjectId("5e88516f987b6e0e9d18f58f"), "Information" : { "FullName" : "Chris Brown" } } 以下は、入力に類似した名前を見つけるためのクエリです-
> db.demo514.find({'Information.FullName': {$regex: "John Doe", $options: 'i'}}); これにより、次の出力が生成されます-
{ "_id" : ObjectId("5e885116987b6e0e9d18f58c"), "Information" : { "FullName" : "John Doe" } }
{ "_id" : ObjectId("5e885169987b6e0e9d18f58e"), "Information" : { "FullName" : "john doe" } } -
正規表現\Javaの構成
部分表現/メタ文字「\A 」は文字列全体の先頭に一致します。 例1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "\\AHi"; String input = "Hi how are you welco
-
Javaの正規表現\Aメタ文字を説明する
部分表現/メタ文字「\A 」は文字列全体の先頭に一致します。 例1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "\\AHi"; String input = "Hi how are you welco