본문 바로가기
카테고리 없음

데이터 베이스(No SQL) - mongoDB 요소 연산자

by nomfang 2021. 1. 23.
728x90
반응형

요소 연산자

$exists 연산자

해당 필드가 존재해야 하는지 존재하지 않아야 하는지 여부를 결정한다

 필드의 값이 없는 경우 값을 추가하지 위해서 주로 사용이 된다

 

#문법 { 필드 명: { $exists: <boolean> } }

#예시) age 필드의 값이 없는 경우를 조회

query = { "age": { "$exists": false } }

$type 연산자

해당 필드의 자료형이 일치하는 도큐먼트를 선택한다

선택 가능한 자료형
double, string, object, array, binData, objectId, bool, date, null, regex, dbPointer, javascript, symbol, javascriptWithScope, int, timestamp, long, minKey, maxKey

#문법 { 필드 명: { $type: <BSON type> } }

#예시) zipcode 필드의 타입이 string인 모든 도큐먼트를 조회

#예시를 위한 가상의 데이터 [ {

"_id" : 1, "address" : "2030 Martian Way", "zipCode" : "90698345" },

{ "_id" : 2, "address": "156 Lunar Place", "zipCode" : 43339374 },

{ "_id" : 3, "address" : "2324 Pluto Place", "zipCode": NumberLong(3921412) },

{ "_id" : 4, "address" : "55 Saturn Ring" , "zipCode" : NumberInt(88602117) },

{ "_id" : 5, "address" : "104 Venus Drive", "zipCode" : ["834847278", "1893289032"]} ]

# 쿼리문 query = { "zipCode" : { "$type" : "string" } }

mydoc = mycol.find(query)

#결과 { "_id" : 1, "address" : "2030 Martian Way", "zipCode" : "90698345" },

{ "_id" : 5, "address" : "104 Venus Drive", "zipCode" : ["834847278", "1893289032"]}

평가 연산자

$mod연산자

나머지를 구하는 연산자

ex) 번호가 짝수인 사람을 모두 찾는 경우에 사용

#문법 { 필드 명: { $mod: [ divisor(나눌값), remainder(나머지) ] } }#예시)

_id가 짝수인 값을 모두 조회

query = { "_id": { "$mod": [2, 0] } } mydoc = mycol.find(query)

$regex연산자

정규표현식 조회를 가능하게 한다

아래 3가지 유형

 $regex만 사용

 $options를 함께 사용

정규표현식(MongoDB는 UTF-8을 지원하는 Perl 호환 정규식 버전 8.42 를 사용한다)처럼 사용해도 된다

#문법 1번째 유형: { 필드 명: { $regex: /pattern/, $options: '<options>' } }

2번째 유형: { 필드 명: { $regex: 'pattern', $options: '<options>' } }

3번째 유형: { 필드 명: { $regex: /pattern/<options> } }

#예시) sku 필드의 789가 있는 데이터를 조회

#예시를 위한 가상의 데이터 [ { "_id" : 100, "sku" : "abc123", "description" : "Single line description." },

{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" },

{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" },

{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" } ]

#쿼리문 query = { "sku": { "$regex": /789$/ } } mydoc = mycol.find(query)

#결과 { "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" },

{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }

$text연산자

텍스트 조회를 하는 연산자

제약사항이 많

대표적으로 필드에 text index가 설정 되어 있어야 한다

꼭 정확한 텍스트가 아니더라도 유사한 텍스트를 찾아준다는 장점

 

#문법 { $text: { $search(문자): <string>, $language(언어): <string>, $caseSensitive(대소문자): <boolean> } }#예시) subject 필드의 값에 "coffee"라는 단어가 포함되어있는 데이터를 조회 #예시를 위한 가상의 데이터 [ { "_id": 1, "subject": "coffee", "author": "xyz", "views": 50 }, {"_id": 2, "subject": "Coffee Shopping", "author": "efg", "views": 5 }, { "_id": 3, "subject": "Baking a cake", "author": "abc", "views": 90 }, { "_id": 4, "subject": "baking", "author": "xyz", "views": 100 }, { "_id": 5, "subject": "Café Con Leche", "author": "abc", "views": 200 }, { "_id": 6, "subject": "Сырники", "author": "jkl", "views": 80 }, { "_id": 7, "subject": "coffee and cream", "author": "efg", "views": 10 }, { "_id": 8, "subject": "Cafe con Leche", "author": "xyz", "views": 10 } ] #쿼리문 query = { "$text": { "$search": "coffee" } } mydoc = mycol.find(query) #결과 { "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg", "views" : 5 } { "_id" : 7, "subject" : "coffee and cream", "author" : "efg", "views" : 10 } { "_id" : 1, "subject" : "coffee", "author" : "xyz", "views" : 50 }

반응형

댓글