개발 일반

(MongoDB) Find (select) 데이터 조회

danune.dev 2022. 6. 9. 14:34

 

지난시간에 학습한 내용

(MongoDB) DB,Collection 생성/관리

 

(MongoDB) DB,Collection 생성/관리

1. DB 관리 1-1. DB 생성 show dbs 1-2. DB 생성 DB를 따로 생성하지 않아도 선택(use) 명령을 내리면 자동으로 생성된다 use myDB 1-3. DB 제거 use myDB db.dropDatabase() 2. Collection 관리 2-1. Collection..

blog.danune.co.kr

(MongoDB) Insert / Update / Delete

 

(MongoDB) Insert / Update / Delete

이전 학습 - 2022.06.09 - [개발 일반] - (MongoDB) DB,Collection 생성/관리 (MongoDB) DB,Collection 생성/관리 1. DB 관리 1-1. DB 생성 show dbs 1-2. DB 생성 DB를 따로 생성하지 않아도 선택(use) 명령을..

blog.danune.co.kr

 

이번엔 MongoDB 에서 데이터를 조회하는 방법을 알아보겠습니다 

 

1. 전체 조회

db.products.find({})

 

2. equal (=) 조건

db.products.find(
    {
        "name": "Chair"
    }
)

3. and 조건

db.products.find(
    {
        $and: [
            { 
                "name": "Chair"
            },
            {
                "price": 150000
            }
        ]
    }
)

4. or 조건

db.products.find(
    {
        $or: [
            { 
                "name": "Chair"
            },
            {
                "name": "CCCCC"
            }
        ]
    }
)

5. and / or 복합 조건

db.products.find({
    $and: [{ 
        $or: [
            { "name": "Chair"},
            {"name": "CCCCC"}
        ]},
        {
            "price": { $gt: 300000 }
        }
    ]
})

6. 문자열 조건 (like) - 정규표현식 이용

6-1. 시작 일치 (C%)

db.products.find({
    "name": /^C/
})

6-2. 중간 일치 (%C%)

db.products.find({
    "name": /C/
})

6-3. 마지막 일치 (%C)

db.products.find({
    "name": /C$/
})

 

7. 크기 조건 

조건 내부 function
크다 $gt
크거나 같다 $gte
작다 $lt
작거나 같다 $lte
같지않다 $ne

7-1. 크다

db.products.find({
    "price": { $gt: 300000 }
})

7-2. 작다

db.products.find({
    "price": { $lt: 300000 }
})

7-3. 같지 않다

db.products.find({
    "price": { $ne: 400000 }
})