문서 메뉴

문서 홈MongoDB 실행 및 관리MongoDB Atlas

배열의 객체에 대해 Atlas Search 쿼리를 실행하는 방법

이 페이지의 내용

embeddedDocuments 튜토리얼에서는 배열( ) 내에 있는 문서 또는 객체의 필드에 대해 Atlas Search 쿼리를 인덱싱하고 실행하는 방법을 설명합니다. 이 페이지에는 Atlas Search 플레이그라운드 에 설정되어 있거나 Atlas cluster 에서 로드, 구성 및 실행할 수 있는 샘플 컬렉션 에 대한 샘플 인덱스 를 사용하여샘플 쿼리 를 실행하는 방법에 대한 지침이 포함되어 있습니다.

샘플 컬렉션의 이름은 schools 이며 여기에는 세 개의 문서가 포함되어 있습니다. 샘플 컬렉션의 각 문서에는 학교의 namemascot , 학교 교사의 firstlast 이름, 각 교사가 가르치는 classes ( subject 이름 및 grade 수준 포함)이 포함되어 있습니다. 및 학교 학생을 위한 다양한 clubs 입니다.

컬렉션에 대한 인덱스 정의는 다음을 보여줍니다.

  • teachersteachers.classes 경로의 배열에 있는 문서는 embeddedDocuments 로 인덱싱되고, 문서 내부의 필드는 동적으로 인덱싱됩니다.

  • teachers 경로의 배열에 있는 문서도 문서 유형으로 인덱싱되어 강조 표시 를 지원하며 문서 내부의 필드가 동적으로 인덱싱됩니다.

  • 필드의 문서는 동적 매핑 clubs 이 활성화된 문서 유형으로 인덱싱되고 필드의 문서 clubs.sports 배열은 embeddedDocuments 동적 매핑이 활성화된 유형으로 인덱싱됩니다.

샘플 쿼리는 schools 컬렉션에 내장된 문서를 검색합니다. 쿼리는 다음 파이프라인 단계를 사용합니다.

  • $search collection을 검색합니다.

  • $project 을(를) 사용하여 컬렉션에서 필드를 포함 및 제외하고 결과에 score 라는 필드를 추가합니다. 강조 표시 를 활성화하는 쿼리의 경우 $project 단계에서는 강조 표시 정보가 포함된 highlights 이라는 새 필드도 추가합니다.

이 튜토리얼에서는 세 가지 쿼리를 보여줍니다.

Atlas Search Playground 에서는 내장된 문서 컬렉션 을 설정하고, 컬렉션의 필드에 대한 인덱스 를 사전 구성하고, 컬렉션에 대해 실행할 수 있는 쿼리 를 정의했습니다. Atlas Search 플레이그라운드에서 컬렉션, 인덱스 및 쿼리를 수정할 수도 있습니다.

이 섹션에서는 내장된 문서에 대해 쿼리를 실행하는 방법을 보여 주기 위해 다음 단계를 안내합니다.

  1. Atlas cluster에 포함된 문서가 있는 schools 이라는 샘플 collection을 만듭니다.

  2. 다음 경로에 embeddedDocuments 필드를 구성하여 Atlas Search 인덱스를 설정합니다.

    • teachers 필드

    • teachers.classes 필드

    • clubs.sports 필드

  3. embeddedDocument 텍스트 $search 연산자와 schools 함께 복합 연산자를 사용하여 컬렉션에 내장된 문서를 검색하는 쿼리를 실행합니다.

  4. 내장된 문서 필드에 대해 $searchMeta 쿼리를 실행하여 개수를 가져옵니다.

시작하기 전에 Atlas 클러스터가 필수구성 요소에 설명된 요건을 충족하는지 확인하세요. 이 튜토리얼에서는 새 컬렉션을 만들고 쿼리를 실행하는 데 필요한 문서를 로드하기 때문에 샘플 데이터를 업로드할 필요가 없습니다.

먼저 Atlas cluster의 기존 데이터베이스 또는 새 데이터베이스에 schools (이)라는 이름의 컬렉션을 만들어야 합니다. 컬렉션을 만든 후에는 샘플 데이터를 컬렉션에 업로드해야 합니다. 샘플 컬렉션의 문서에 대해 자세히 알아보려면 샘플 컬렉션 정보를 참조하세요.

이 섹션에서는 새 데이터베이스 및 컬렉션을 생성하고 컬렉션에 샘플 데이터를 로드하는 과정을 안내합니다.

1
  1. 클러스터에 로그인합니다.

  2. 클러스터 이름을 클릭합니다.

  3. Collections를 클릭합니다.

2
  1. 새 데이터베이스를 만들려면 Create Database 를 클릭합니다.

  2. 데이터베이스 이름과 컬렉션 이름을 입력합니다.

    • Database Name 필드에 local_school_district를 지정합니다.

    • Collection Name 필드에 schools를 지정합니다.

3
  1. Collection이 선택되지 않은 경우 schools collection을 선택합니다.

  2. 컬렉션에 추가할 각 샘플 문서에 대해 Insert Document 를 클릭합니다.

  3. JSON 보기({})를 클릭하여 기본 문서를 바꿉니다.

  4. 다음 샘플 문서를 한 번에 하나씩 복사하여 붙여넣고 Insert을 클릭하여 컬렉션에 문서를 한 번에 하나씩 추가합니다.

    {
    "_id": 0,
    "name": "Springfield High",
    "mascot": "Pumas",
    "teachers": [{
    "first": "Jane",
    "last": "Smith",
    "classes": [{
    "subject": "art of science",
    "grade": "12th"
    },
    {
    "subject": "applied science and practical science",
    "grade": "9th"
    },
    {
    "subject": "remedial math",
    "grade": "12th"
    },
    {
    "subject": "science",
    "grade": "10th"
    }]
    },
    {
    "first": "Bob",
    "last": "Green",
    "classes": [{
    "subject": "science of art",
    "grade": "11th"
    },
    {
    "subject": "art art art",
    "grade": "10th"
    }]
    }],
    "clubs": {
    "stem": [
    {
    "club_name": "chess",
    "description": "provides students opportunity to play the board game of chess informally and competitively in tournaments."
    },
    {
    "club_name": "kaboom chemistry",
    "description": "provides students opportunity to experiment with chemistry that fizzes and explodes."
    }
    ],
    "arts": [
    {
    "club_name": "anime",
    "description": "provides students an opportunity to discuss, show, and collaborate on anime and broaden their Japanese cultural understanding."
    },
    {
    "club_name": "visual arts",
    "description": "provides students an opportunity to train, experiment, and prepare for internships and jobs as photographers, illustrators, graphic designers, and more."
    }
    ]
    }
    }
    {
    "_id": 1,
    "name": "Evergreen High",
    "mascot": "Jaguars",
    "teachers": [{
    "first": "Jane",
    "last": "Earwhacker",
    "classes": [{
    "subject": "art",
    "grade": "9th"
    },
    {
    "subject": "science",
    "grade": "12th"
    }]
    },
    {
    "first": "John",
    "last": "Smith",
    "classes": [{
    "subject": "math",
    "grade": "12th"
    },
    {
    "subject": "art",
    "grade": "10th"
    }]
    }],
    "clubs": {
    "sports": [
    {
    "club_name": "archery",
    "description": "provides students an opportunity to practice and hone the skill of using a bow to shoot arrows in a fun and safe environment."
    },
    {
    "club_name": "ultimate frisbee",
    "description": "provides students an opportunity to play frisbee and learn the basics of holding the disc and complete passes."
    }
    ],
    "stem": [
    {
    "club_name": "zapped",
    "description": "provides students an opportunity to make exciting gadgets and explore electricity."
    },
    {
    "club_name": "loose in the chem lab",
    "description": "provides students an opportunity to put the scientific method to the test and get elbow deep in chemistry."
    }
    ]
    }
    }
    {
    "_id": 2,
    "name": "Lincoln High",
    "mascot": "Sharks",
    "teachers": [{
    "first": "Jane",
    "last": "Smith",
    "classes": [{
    "subject": "science",
    "grade": "9th"
    },
    {
    "subject": "math",
    "grade": "12th"
    }]
    },
    {
    "first": "John",
    "last": "Redman",
    "classes": [{
    "subject": "art",
    "grade": "12th"
    }]
    }],
    "clubs": {
    "arts": [
    {
    "club_name": "ceramics",
    "description": "provides students an opportunity to acquire knowledge of form, volume, and space relationships by constructing hand-built and wheel-thrown forms of clay."
    },
    {
    "club_name": "digital art",
    "description": "provides students an opportunity to learn about design for entertainment, 3D animation, technical art, or 3D modeling."
    }
    ],
    "sports": [
    {
    "club_name": "dodgeball",
    "description": "provides students an opportunity to play dodgeball by throwing balls to eliminate the members of the opposing team while avoiding being hit themselves."
    },
    {
    "club_name": "martial arts",
    "description": "provides students an opportunity to learn self-defense or combat that utilize physical skill and coordination without weapons."
    }
    ]
    }
    }

이 섹션에서는 local_school_district.schools 컬렉션의 내장된 문서에 있는 필드에 대한 Atlas Search 인덱스를 만듭니다.

Atlas Search 인덱스를 생성하려면 프로젝트에 대한 Project Data Access Admin 이상의 액세스 권한이 있어야 합니다.

1
  1. 이미 표시되어 있지 않은 경우 탐색 모음의 Organizations 메뉴에서 원하는 프로젝트가 포함된 조직을 선택합니다.

  2. 이미 표시되어 있지 않은 경우 Projects 탐색 모음의 프로젝트 메뉴에서 원하는 프로젝트를 선택합니다.

  3. Database Deployments 페이지가 아직 표시되지 않은 경우 사이드바에서 Database를 클릭합니다.

2
  1. 클러스터 이름을 클릭합니다.

  2. Atlas Search 탭을 클릭합니다.

3
4
  • 안내를 따라 진행하고자 할 때는 Visual Editor를 선택합니다.

  • 원시 인덱스 정의를 수정하려면 JSON Editor를 선택합니다.

5
  1. Index Name 필드에 embedded-documents-tutorial를 입력합니다.

    참고

    인덱스 이름을 default으로 지정하면 $search 파이프라인 단계를 사용할 때 index 매개 변수를 지정할 필요가 없습니다. 그렇지 않으면 index 매개 변수를 사용하여 인덱스 이름을 지정해야 합니다.

  2. Database and Collection 섹션에서 local_school_district 데이터베이스를 찾고 schools 컬렉션을 선택합니다.

6

인덱스 정의에 대해 자세히 알아보려면 Atlas Search 인덱스 정보를 참조하세요.

7

Atlas에서 인덱스가 작성 중임을 알려주는 모달 창이 표시됩니다.

8

인덱스 작성에는 약 1분 정도가 소요됩니다. 작성하는 동안 Status 열은 Build in Progress을 읽습니다. 작성이 완료되면 Status 열에 Active이 표시됩니다.

내장된 문서 필드에 대해 쿼리를 실행할 수 있습니다. 이 튜토리얼에서는 쿼리에서 복합 연산자 내부의 embeddedDocument텍스트 연산자를 사용합니다.

이 섹션에서는 Atlas 클러스터에 연결하고 schools 컬렉션의 필드에 대해 연산자를 사용하여 샘플 쿼리를 실행합니다.


➤ 이 페이지의 언어 선택 드롭다운 메뉴를 사용하여 이 섹션에 있는 예제의 언어를 설정합니다.


내장된 문서 필드에 대해 $searchMeta 쿼리를 실행할 수 있습니다. 이 섹션에서는 Atlas 클러스터에 연결하고 내장된 문서 필드에서 $searchMeta 단계 및 패싯을 사용하여 샘플 쿼리를 실행합니다.

← Atlas Search $search 쿼리로 $unionWith를 실행하는 방법