Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
C 드라이버
/

쿼리 지정

이 가이드 에서는 C 운전자 를 사용하여 쿼리 를 지정하는 방법을 학습 수 있습니다.

쿼리 수행할 때 컬렉션 에서 특정 문서를 조회 하도록 쿼리 필터하다 정의할 수 있습니다. 쿼리 필터하다 는 MongoDB 읽기 또는 쓰기 (write) 작업에서 문서를 일치시키는 데 사용하는 검색 기준을 지정하는 표현식 입니다. 쿼리 필터하다 정의하여 운전자 쿼리 와 정확히 일치하는 문서를 검색 하도록 지시하거나 보다 복잡한 일치 기준을 Express 하도록 쿼리 필터를 작성할 수 있습니다.

이 가이드 의 예제에서는 Atlas 샘플 데이터 세트sample_mflix 데이터베이스 에 있는 movies 컬렉션 을 사용합니다. 무료 MongoDB Atlas cluster 를 생성하고 샘플 데이터 세트를 로드하는 방법을 학습 보려면 Atlas 시작하기 가이드 를 참조하세요.

리터럴 값 쿼리는 쿼리 필터와 정확히 일치하는 문서를 반환합니다.

다음 예시 쿼리 필터하다 mongoc_collection_find_with_opts() 함수에 대한 매개 변수로 지정합니다. 이 코드는 type 필드 값이 "movie"인 모든 문서를 반환합니다.

const bson_t *doc;
bson_t *filter = BCON_NEW ("type", BCON_UTF8 ("movie"));
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : { "$oid" : "..." }, "title" : "Wild and Woolly", "type" : "movie", ... }
{ "_id" : { "$oid" : "..." }, "title" : "The Devil to Pay!", "type" : "movie", ... }
{ "_id" : { "$oid" : "..." }, "title" : "Traffic in Souls", "type" : "movie", ... }
{ "_id" : { "$oid" : "..." }, "title" : "Now or Never", "type" : "movie", ... }
{ "_id" : { "$oid" : "..." }, "title" : "High and Dizzy", "type" : "movie", ... }
...

비교 연산자는 쿼리 필터의 지정된 값을 기준으로 문서 필드 값을 평가합니다. 다음은 일반적인 비교 연산자 목록입니다.

  • $gt: 보다 큼

  • $lte: 보다 작거나 같음

  • $ne: 같지 않음

비교 연산자의 전체 목록을 보려면 MongoDB Server 매뉴얼의 비교 쿼리 연산자 가이드를 참조하세요.

다음 예시 쿼리 필터하다 의 비교 연산자 mongoc_collection_find_with_opts() 함수에 대한 매개 변수로 지정합니다. 이 코드는 year 필드 의 값이 2015보다 큰 모든 문서를 반환합니다.

const bson_t *doc;
bson_t *filter = BCON_NEW ("year", "{", "$gt", BCON_INT32 (2015), "}");
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : ..., "title" : "The Masked Saint", "year" : { "$numberInt" : "2016" }, ... }

논리 연산자는 두 개 이상의 표현식 집합의 결과에 적용된 논리를 사용하여 문서를 일치시킵니다. 다음은 논리 연산자 목록입니다.

  • $and모든 절의 조건과 일치하는 모든 문서를 반환합니다.

  • $or 절의 조건과 일치하는 모든 문서를 반환합니다.

  • $nor, 어떤 절의 조건과도 일치 하지 않는 모든 문서를 반환합니다.

  • $not표현식과 일치 하지 않는 모든 문서를 반환합니다.

논리 연산자에 대해 자세히 알아보려면 MongoDB Server 매뉴얼의 논리 쿼리 연산자 가이드를 참조하세요.

다음 예시 쿼리 필터하다 의 논리 연산자 mongoc_collection_find_with_opts() 함수에 대한 매개 변수로 지정합니다. 이 코드는 필드 값이 year 1983 또는 인 모든 문서를 1985 반환합니다.

const bson_t *doc;
bson_t *filter = BCON_NEW (
"$or", "[",
"{", "year", BCON_INT64 (1983), "}",
"{", "year", BCON_INT64 (1985), "}",
"]"
);
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : ..., "title" : "Amityville 3-D", "year" : { "$numberInt" : "1983" }, ... }
{ "_id" : ..., "title" : "Barefoot Gen", "year" : { "$numberInt" : "1983" }, ... }
{ "_id" : ..., "title" : "Betrayal", "year" : { "$numberInt" : "1983" }, ... }
{ "_id" : ..., "title" : "You're a Good Man, Charlie Brown", "year" : { "$numberInt" : "1985" }, ... }
{ "_id" : ..., "title" : "Yes: 9012 Live", "year" : { "$numberInt" : "1985" }, ... }
...

배열 연산자는 배열 필드에 있는 요소의 값 또는 수량을 기준으로 문서를 일치시킵니다. 다음은 사용 가능한 배열 연산자 목록입니다.

  • $all쿼리의 모든 요소를 포함하는 배열이 있는 문서를 반환합니다.

  • $elemMatch배열 필드의 요소가 쿼리의 모든 조건과 일치하는 경우 문서를 반환합니다.

  • $size지정된 크기의 배열이 있는 모든 문서를 반환합니다.

배열 연산자에 대해 자세히 알아보려면 매뉴얼의 배열 쿼리 연산자 가이드를 MongoDB Server 참조하세요.

다음 예시 쿼리 필터하다 의 배열 연산자 mongoc_collection_find_with_opts() 함수에 대한 매개 변수로 지정합니다. 이 코드는 genres 배열 필드 의 값에 정확히 2 개의 요소가 포함된 모든 문서를 반환합니다.

const bson_t *doc;
bson_t *filter = BCON_NEW ("genres", "{", "$size", BCON_INT32 (2), "}");
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : ..., "genres" : [ "Comedy", "Romance" ], "title" : "The Devil to Pay!", ... }
{ "_id" : ..., "genres" : [ "Crime", "Drama" ], "title" : "Traffic in Souls", ... }
{ "_id" : ..., "genres" : [ "Comedy", "Short" ], "title" : "High and Dizzy", ... }
{ "_id" : ..., "genres" : [ "Comedy", "Short" ], "title" : "Now or Never", ... }
{ "_id" : ..., "genres" : [ "Drama", "Romance" ], "title" : "A Woman of Paris: A Drama of Fate", ... }
...

요소 연산자는 필드의 존재 여부 또는 유형에 따라 데이터를 쿼리합니다.

요소 연산자에 대해 자세히 알아보려면 매뉴얼의 요소 쿼리 연산자 가이드를 MongoDB Server 참조하세요.

다음 예시 쿼리 필터하다 의 $exists 연산자 mongoc_collection_find_with_opts() 함수에 대한 매개 변수로 지정합니다. 이 코드는 num_mflix_comments 필드 있는 모든 문서를 반환합니다.

const bson_t *doc;
bson_t *filter = BCON_NEW ("num_mflix_comments", "{", "$exists", BCON_BOOL (true), "}");
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : ..., "num_mflix_comments" : { "$numberInt" : "0" }, "title" : "The Park Is Mine", ...}
{ "_id" : ..., "num_mflix_comments" : { "$numberInt" : "1" }, "title" : "The Good Father", ... }
{ "_id" : ..., "num_mflix_comments" : { "$numberInt" : "0" }, "title" : "Alpine Fire", ... }
{ "_id" : ..., "num_mflix_comments" : { "$numberInt" : "1" }, "title" : "Huang jia shi jie", ... }
{ "_id" : ..., "num_mflix_comments" : { "$numberInt" : "0" }, "title" : "Twenty Years Later", ... }
...

평가 연산자는 개별 필드 또는 전체 컬렉션 문서의 평가를 기반으로 데이터를 반환합니다.

다음은 일반적인 평가 연산자 목록입니다.

  • $text문서에 대해 텍스트 Atlas Search를 수행합니다.

  • $regex지정된 정규 표현식과 일치하는 문서를 반환합니다.

  • $mod모듈로 를 수행합니다. 필드 값에 대한 연산을 수행하고 나머지가 지정된 값인 문서를 반환합니다.

평가 연산자의 전체 목록을 보려면 MongoDB Server 매뉴얼의 평가 쿼리 연산자 가이드를 참조하세요.

다음 예시 쿼리 필터하다 의 평가 연산자 mongoc_collection_find_with_opts() 함수에 대한 매개 변수로 지정합니다. 이 코드는 정규 표현식 사용하여 title 필드 값에 연속된 "p" 문자가 두 개 이상 있는 모든 문서를 반환합니다.

const bson_t *doc;
bson_t *filter = BCON_NEW("title", "{", "$regex", BCON_UTF8("p{2,}"), "}");
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : ..., "title" : "He Who Gets Slapped", ... }
{ "_id" : ..., "title" : "David Copperfield", ... }
{ "_id" : ..., "title" : "Applause", ... }
{ "_id" : ..., "title" : "Skippy", ... }
{ "_id" : ..., "title" : "This Happy Breed", ... }
...

문서 쿼리에 대해 자세히 알아보려면 MongoDB Server 매뉴얼의 문서 쿼리 가이드를 참조하세요.

C 운전자 사용하여 문서를 조회 에 대해 자세히 학습 데이터 검색을 참조하세요.

함수에 대해 자세히 학습 API mongoc_collection_find_with_opts() 설명서를 참조하세요.

돌아가기

데이터 읽기

이 페이지의 내용