ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [MongoDB] MongoDB - 문자열 연산자, LIKE 검색
    NoSQL/MongoDB 2022. 7. 5. 17:17
    반응형

    Example Collection

    db.products.insertMany([
    	{ "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate" : ISODate("2011-05-14T00:00:00Z"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 }, "color" : [ "white", "black" ], "storage" : [ 64, 128, 256 ] },
    	{ "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate" : ISODate("2011-09-01T00:00:00Z"), "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 }, "color" : [ "white", "black", "purple" ], "storage" : [ 128, 256, 512 ] },
    	{ "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate" : ISODate("2015-01-14T00:00:00Z"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 }, "color" : [ "blue" ], "storage" : [ 16, 64, 128 ] },
    	{ "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate" : ISODate("2020-05-14T00:00:00Z"), "spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 }, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256, 1024 ] },
    	{ "_id" : 5, "name" : "SmartPhone", "price" : 599, "releaseDate" : ISODate("2022-09-14T00:00:00Z"), "spec" : { "ram" : 4, "screen" : 9.7, "cpu" : 1.66 }, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256 ] },
    	{ "_id" : 6, "name" : "xWidget", "spec" : { "ram" : 64, "screen" : 9.7, "cpu" : 3.66 }, "color" : [ "black" ], "storage" : [ 1024 ] }
    ])

     

    $regex : 값이 정규 표현식과 맞는 도큐먼트를 조회

    $regex 연산자에는 옵션 값을 설정할 수 있기 때문에 연산자의 형식이 세 가지입니다.


    1. $regex 연산자의 값으로 정규 표현식을 갖고, $options 연산자를 추가하는 방식

    { <field>: { $regex: /pattern/, $options: '<options>' } }

    예제: name 값에 'phone' 문자열을이 포함되는 도큐먼트를 조회

    db.getCollection('products').find({'name': {'$regex': /phone/, '$options': 'i'}})

     

    2. $regex 연산자의 값으로 문자열을 갖고, $options 연산자를 추가하는 방식

    { <field>: { $regex: 'pattern', $options: '<options>' } }

    예제: name 값에 'phone' 문자열을이 포함되는 도큐먼트를 조회

    db.getCollection('products').find({'name': {'$regex': 'phone', '$options': 'i'}})


    3. $regex 연산자의 값으로 일반적인 정규 표현식과 같이 정규 표현식 내부에 욥션을 표시하는 방식

    { <field>: { $regex: /pattern/<options> }

    예제: name 값에 'phone' 문자열을이 포함되는 도큐먼트를 조회

    db.getCollection('products').find({'name': {'$regex': /phone/i}})

     

    $options 연산자의 옵션 값

    옵션 설명
    i 대소문자 무시
    m 정규식에서 앵커(^)를 사용할 때 값에 \n 이 있다면 무시
    x 정규식 안에 있는 공백을 무시
    s 점(.)을 사용할 때 \n 을 포함해서 매치


    $regex 연산자를 사용하지 않고, 필드의 값으로 정규 표현식(/.../)을 사용하여 문자열 검색할 수 있습니다.
    예제: name 값에 'phone' 문자열을이 포함되는 도큐먼트를 조회

    db.getCollection('products').find({'name': /phone/i })


    필드에 여러 연산자를 적용할 때 $regex 연산자를 사용합니다.
    예제: name 값에 'phone' 문자열을 포함하고, 'smart' 문자열을는 포함하지 않는 도큐먼트를 조회

    db.getCollection('products').find({'name': {'$regex': /phone/i, '$not': /smart/i}})

     

    LIKE 검색

    예제: name 값에 'phone' 문자열을 포함하는 도큐먼트를 조회

    db.getCollection('products').find({'name': /phone/i })

     

    예제: name 값에 'smart'로 시작하는 문자열을 포함하는 도큐먼트를 조회 

    db.getCollection('products').find({'name': /^smart/i })

    시작하는 문자열을 찾는 정규표현식 ^을 사용

     


    예제: name 값에 'pad'으로 끝나는 문자열을 포함하는 도큐먼트를 조회 // Like '%pad'

    db.getCollection('products').find({'name': /pad$/i })

    끝나는 문자열을 찾는 정규표현식 &을 사용

     

     

    참고 : https://www.mongodb.com/docs/manual/reference/operator/query/

    참고 : https://stackoverflow.com/questions/3305561/how-to-query-mongodb-with-like

     

    반응형

    댓글

Designed by Tistory.