# Exercises

## Exercise 1

### Preliminary

* Import `people.json` into `lab4.people` (i.e. database name is `lab4` and collection name is `people`)

```bash
$ mongoimport -d lab4 -c people people.json
```

### Questions

* Find the documents whose age under 22.
* Find the documents whose age is between 22 and 26 (inclusive).
* Find the document whose name is “Lee” suppressing the profile.
* Find the documents whose name is not Lee.
* Find the documents whose name is “Lee” or “Park”.
* Find the documents whose name is not “Kim” or “Park” or “Lee”.

## Exercise 2

### Preliminary

* Import `inventory.json` into lab4.inventory

### Questions

* Find documents where the *tags* field contains below elements.
  * appliance, school, and book.
  * Hint: recall `$all` operator
* Find all documents but select at most three items in the *tags* field.
  * Hint: recall `$slice` operator
* Find all documents but select items from the second to the fourth in the *tags* field.
  * Hint: recall `$slice` operator with `[skip, limit]`
* Find documents that have brown color in the *qty* field.
  * Hint: recall `$elemMatch` operator

## Exercise 3

### Preliminary

* Import `store.json` to the store collection

### Questions

* Find documents where *buyer*'s *name* is "J.S. Lee"
  * Hint: Querying embedded document.
* Print purchased *items* where a *name* in a document is "iPhone Xs"
  * Hint: Querying embedded document, Projection
* Print the buyer’s purchase method where *items* contain the document that price is more than 2000$
  * Hint: < array.field >, Projection
* Find documents that don’t have carrier as key in attribute where the name is "Apple Watch Series 4"
  * Hint: `$elemMatch`, `$exists`

## **Exercise 4**

### **Preliminary**

* Import `store.json` to the store collection
  * Same with Exercise 3!

### Questions

* Find documents where buyer's name is "J.S. Lee"
  * Hint: Querying embedded document.&#x20;
* Print purchased items where a name in a document is "iPhone Xs".&#x20;
  * Hint: Querying embedded document, Projection&#x20;
* Print the buyer’s purchase method where items contain the document that price is more than 2000$.&#x20;
  * Hint: `<array.field>`, Projection&#x20;
* Find documents that don’t have carrier as key in attribute where the name is "Apple Watch Series 4"&#x20;
  * Hint: `$elemMatch`, `$exists`

## **Exercise 5**

### Preliminary

* Import `store.json` to the store collection
  * Same with Exercise 3!

### Questions

* Get the first three documents and print buyers field only.
* Skip a document and then print two documents
* Count documents in the store collection.

## **Exercise 6**

### Preliminary

* Using `grade.json` (import it using mongoimport)

### Questions

* The grade’s document can have among three types:
  * quiz, exam or homework
* Student id (sid) is from 0 to 99
* A document is lost, which contains a quiz score\
  1\. print lost document’s sid.\
  2\. Insert the quiz score (80) to the corresponding sid\
  3\. print the three students that have the highest quiz score.
* A simple example of the dataset looks like ...

```javascript
{"sid": 0, "type": "homework", "score": 69}
{"sid": 0, "type": "quiz" , "score": 29}
{"sid": 0, "type": "exam", "score": 86}
{"sid": 1, "type": "homework", "score": 2}
{"sid": 1, "type": "quiz" , "score": 6}
{"sid": 1, "type": "exam", "score": 67}
{"sid": 2, "type": "homework", "score": 13}
{"sid": 2, "type": "quiz" , "score": 91}
{"sid": 2, "type": "exam", "score": 76}
...
```

## **Exercise 7**

### Preliminary

* Using `grade.json` (import it using mongoimport)
  * Same with exercise 6

### Questions

* Write the score searching script
  * Search documents by filtering *type* and *score* fields

### Results

```
search > homework 13
sid homework
2 13
74 13
```

```
search > quiz 41-49
sid quiz
10 46
17 48
19 44
...
96 49
98 41
```

\
&#x20;
------


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://koonkim.gitbook.io/mongodb/master/crud/read/exercises.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
