과목/빅데이터
MongoDB 문장 속에 있는 일치하는 단어 찾기 regex pattern for finding a specific word that belong somewhere.
원펀만
2018. 8. 11. 22:44
db.products.find( { sku: { $regex: /789$/ } } )
database의 collection이 products, field가 sku인 데이터 중에서 789로 끝나는 단어 검색
ex abc789, aaaa789, 789789, 789
SELECT * FROM products WHERE sku like "%789";
SQL에서는 위와 같이 사용
db.products.find( { sku: { $regex: /^ABC/i } } )
field가 sku인 데이터 중에서 ABC로 시작하는 단어 검색, i는 case-insentive(대소문자 구분 안함), ABC나 abc 등이 가능
ex ABC, ABC123, abc123
다른 방법
1 | db.listings.aggregate([{$match:{field:{$regex:/^경리/}}}]) | cs |
https://docs.mongodb.com/manual/reference/operator/query/regex/