1000 レコードのコレクションに文字列列があります。
mongodb のクエリに Jongo API を使用しています。
列文字列が文字「AB」で始まり、スラッシュ「/」で終わる、一致するレコードを見つける必要があります。
同じものを選択するためにクエリを実行するには、クエリのヘルプが必要です。
ありがとう。
Jongo API で正規表現を使用してクエリを実行する方法を知っていて、そのために必要な正規表現を探しているだけだと思いますか?
その場合、この正規表現は、「AB」(大文字と小文字を区別) で始まり、その後に任意の数の他の文字が続き、スラッシュ (「/」) で終わる文字列を検索します。
^AB.*\/$
^ - matches the start of the string
AB - matches the string 'AB' exactly
.* - matches any character ('.') any number of times ('*')
\/ - matches the literal character '/' (backslash is the escape character)
$ - matches the end of the string
正規表現を始めたばかりの場合は、Regex 101 Web サイトを強くお勧めします。これは、正規表現をテストするための素晴らしいサンドボックスであり、式の各ステップを説明してデバッグをより簡単にします。
私は解決策を見つけ、以下はうまくいきました。
db.getCollection('employees').find({employeeName:{$regex: '^Raju.*\\/$'}})
db.getCollection('employees').find({employeeName:{$regex: '\/$'}})
getCollection().find("{employeeName:{$regex:
'^Raju.*\\\\/$'}}").as(Employee.class);
getCollection().find("{employeeName:{$regex: '\\/$'}}").as(Employee.class);
getCollection().find("{employeeName:#}",
Pattern.compile("\\/$")).as(Employee.class);
getCollection().find("{"Raju": {$regex: #}}", "\\/$").as(Employee.class);
map = new HashMap<>();
map.put("employeeName", Pattern.compile("\\/$"));
coll = getCollection().getDBCollection().find(new BasicDBObject(map));
これを試すことができます。
public List<Product> searchProducts(String keyword) {
MongoCursor<Product> cursor = collection.find("{name:#}", Pattern.compile(keyword + ".*")).as(Product.class);
List<Product> products = new ArrayList<Product>();
while (cursor.hasNext()) {
Product product = cursor.next();
products.add(product);
}
return products;
}