본문 바로가기
Databases/MongoDB | Mongoose

[MongoDB] CRUD - QUERY (READ)

by CodeMia 2021. 11. 15.

 READ 

collection에서 documents를 검색(retrieve) 해준다. 

 

1. 모든 데이터 다 불러오기 

 db.collection.find() 

"_id" value는 항상 return 해준다. 

 

 

 

2. collection에서 일부 데이터만 뽑아오기 

db.collection.find(query, projection)

원래는 query와 projection 2 가지의 파라미터가 있다. 이 둘은 옵션이라서 안 적어줘도 된다. 

 

query 란?

✔️ 검색을 하기 위해 정해진 질의어.

✔️ document 타입이다

✔️ optional이다.

✔️ 파라미터 자리에 query 안쓰면 collection 안 전체 document가 리턴된다. 

✔️ query operatiors를 사용해 구체적으로 검색가능하다.

 

products collection 안에서 name이 Pencil인 document 찾기 

 

price가 0.5보다 큰 document 찾기 

 

 

projection 이란?

✔️ document 타입이다.

✔️ optional이다.

✔️ 검색된 query 조건 안에서 한 번 더 필터를 걸쳐 field를 써주면 value를 알려 준다. 

 

0은 false, 1은 true 이다.

false 이면 value 리턴 안해주고, true면 value 리턴해준다. 

 

"_id" value는 항상 return 해주는데 받고 싶지 않다면 false를 적어준다. 

id는 나오지 않았다

 

 

 


Query Selectors

https://docs.mongodb.com/manual/reference/operator/

 

Comparison

For comparison of different BSON type values, see the specified BSON comparison order.

NameDescription
{price:{$eg: 10}} price가 10인 document들 나와라 
{price:{$gt: 10}} price가 10보다 더 큰 document들 나와라 
{price:{$gte: 10}} price가 10과 같거나 더 큰 document들 나와라 
Matches any of the values specified in an array.
Matches values that are less than a specified value.
Matches values that are less than or equal to a specified value.
Matches all values that are not equal to a specified value.
Matches none of the values specified in an array.

Logical

NameDescription
Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
Inverts the effect of a query expression and returns documents that do not match the query expression.
Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
Joins query clauses with a logical OR returns all documents that match the conditions of either clause.

Element

NameDescription
Matches documents that have the specified field.
Selects documents if a field is of the specified type.

Evaluation

NameDescription
Allows use of aggregation expressions within the query language.
Validate documents against the given JSON Schema.
Performs a modulo operation on the value of a field and selects documents with a specified result.
Selects documents where values match a specified regular expression.
Performs text search.
Matches documents that satisfy a JavaScript expression.

Geospatial

NameDescription
Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.
Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin.
Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.
Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.

Array

NameDescription
Matches arrays that contain all elements specified in the query.
Selects documents if element in the array field matches all the specified $elemMatch conditions.
Selects documents if the array field is a specified size.

Bitwise

NameDescription
Matches numeric or binary values in which a set of bit positions all have a value of 0.
Matches numeric or binary values in which a set of bit positions all have a value of 1.
Matches numeric or binary values in which any bit from a set of bit positions has a value of 0.
Matches numeric or binary values in which any bit from a set of bit positions has a value of 1.

Projection Operators

NameDescription
Projects the first element in an array that matches the query condition.
Projects the first element in an array that matches the specified $elemMatch condition.
Projects the document's score assigned during $text operation.
Limits the number of elements projected from an array. Supports skip and limit slices.

Miscellaneous Operators

NameDescription
Adds a comment to a query predicate.
Generates a random float between 0 and 1.

 

 

 

 

댓글