|
| 1 | +--- |
| 2 | +title: Baseline Testing |
| 3 | +weight: 5 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +Since MongoDB is installed successfully on your GCP C4A Arm virtual machine, follow these steps to validate that the server is running and accepting local connections. |
| 11 | + |
| 12 | +## MongoDB Baseline Testing (Using **mongosh**) |
| 13 | + |
| 14 | +1. Connect to MongoDB |
| 15 | + |
| 16 | +Open a shell session to the local MongoDB instance: |
| 17 | +```console |
| 18 | +mongosh mongodb://127.0.0.1:27017 |
| 19 | +``` |
| 20 | + |
| 21 | +2. Create a Test Database and Collection: |
| 22 | + |
| 23 | +```console |
| 24 | +use baselineDB |
| 25 | +db.createCollection("test") |
| 26 | +``` |
| 27 | +This creates a new database **baselineDB** and an empty collection named test. |
| 28 | + |
| 29 | +You should see an output similar to: |
| 30 | + |
| 31 | +```output |
| 32 | +test> use baselineDB |
| 33 | +... db.createCollection("test") |
| 34 | +... |
| 35 | +switched to db baselineDB |
| 36 | +``` |
| 37 | +3. Insert 10,000 Test Documents: |
| 38 | + |
| 39 | +```javascript |
| 40 | +for (let i = 0; i < 10000; i++) { |
| 41 | + db.test.insertOne({ |
| 42 | + record: i, |
| 43 | + status: "new", |
| 44 | + timestamp: new Date() |
| 45 | + }) |
| 46 | +} |
| 47 | +``` |
| 48 | +This simulates basic write operations with timestamped records. |
| 49 | +10,000 documents will be cretaed and inserted into the test collection of the currently selected database. |
| 50 | +The record field would increment from 0 to 9999. The status is always "new". |
| 51 | +The timestamp would capture the insertion time for each document using ***new Date()***. |
| 52 | + |
| 53 | +You should see an output similar to: |
| 54 | + |
| 55 | +```output |
| 56 | +{ |
| 57 | + acknowledged: true, |
| 58 | + insertedId: ObjectId('6892dacfbd44e23df4750aa9') |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +4. Read (Query) a Subset of Documents: |
| 63 | + |
| 64 | +Fetch a few documents to verify read functionality. |
| 65 | +```javascript |
| 66 | +db.test.find({ status: "new" }).limit(5) |
| 67 | +``` |
| 68 | +This command is a simple read operation to verify that your data is inserted correctly. It queries the test collection in the current database, and only returns documents where the status is "new". ***limit(5)*** returns only the first 5 matching documents. |
| 69 | + |
| 70 | +You should see an output similar to: |
| 71 | + |
| 72 | +```output |
| 73 | +[ |
| 74 | + { |
| 75 | + _id: ObjectId('6892dacbbd44e23df474e39a'), |
| 76 | + record: 0, |
| 77 | + status: 'new', |
| 78 | + timestamp: ISODate('2025-08-06T04:32:11.090Z') |
| 79 | + }, |
| 80 | + { |
| 81 | + _id: ObjectId('6892dacbbd44e23df474e39b'), |
| 82 | + record: 1, |
| 83 | + status: 'new', |
| 84 | + timestamp: ISODate('2025-08-06T04:32:11.101Z') |
| 85 | + }, |
| 86 | + { |
| 87 | + _id: ObjectId('6892dacbbd44e23df474e39c'), |
| 88 | + record: 2, |
| 89 | + status: 'new', |
| 90 | + timestamp: ISODate('2025-08-06T04:32:11.103Z') |
| 91 | + }, |
| 92 | + { |
| 93 | + _id: ObjectId('6892dacbbd44e23df474e39d'), |
| 94 | + record: 3, |
| 95 | + status: 'new', |
| 96 | + timestamp: ISODate('2025-08-06T04:32:11.104Z') |
| 97 | + }, |
| 98 | + { |
| 99 | + _id: ObjectId('6892dacbbd44e23df474e39e'), |
| 100 | + record: 4, |
| 101 | + status: 'new', |
| 102 | + timestamp: ISODate('2025-08-06T04:32:11.106Z') |
| 103 | + } |
| 104 | +] |
| 105 | +``` |
| 106 | +5. Update a Document: |
| 107 | + |
| 108 | +Update a specific document's field to validate update capability. |
| 109 | +```javascript |
| 110 | +db.test.updateOne({ record: 100 }, { $set: { status: "processed" } }) |
| 111 | +``` |
| 112 | +Above command will find the first document where record is exactly 100, and updates that document by setting its status field to "processed". |
| 113 | + |
| 114 | +You should see an output similar to: |
| 115 | + |
| 116 | +```output |
| 117 | +{ |
| 118 | + acknowledged: true, |
| 119 | + insertedId: null, |
| 120 | + matchedCount: 1, |
| 121 | + modifiedCount: 1, |
| 122 | + upsertedCount: 0 |
| 123 | +} |
| 124 | +``` |
| 125 | +6. View the Updated Document Before Deletion |
| 126 | + |
| 127 | +```console |
| 128 | +db.test.findOne({ record: 100 }) |
| 129 | +``` |
| 130 | +This retrieves the document where record is 100, allowing you to verify that its status has been updated to "processed". |
| 131 | + |
| 132 | +You should see output similar to: |
| 133 | + |
| 134 | +```output |
| 135 | +{ |
| 136 | + _id: ObjectId('689490ddb7235c65ca74e3fe'), |
| 137 | + record: 100, |
| 138 | + status: 'processed', |
| 139 | + timestamp: ISODate('2025-08-07T11:41:17.508Z') |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +7. Delete a Document: |
| 144 | + |
| 145 | +```javascript |
| 146 | +db.test.deleteOne({ record: 100 }) |
| 147 | +``` |
| 148 | +This tells MongoDB to delete one document from the test collection, where record is exactly 100. |
| 149 | + |
| 150 | +You should see an output similar to: |
| 151 | + |
| 152 | +```output |
| 153 | +{ acknowledged: true, deletedCount: 1 } |
| 154 | +``` |
| 155 | +Now, confirm the deletion: |
| 156 | + |
| 157 | +```console |
| 158 | +db.test.findOne({ record: 100 }) |
| 159 | +``` |
| 160 | +The above command confirms that the document was successfully deleted. |
| 161 | + |
| 162 | +You should see an output similar to: |
| 163 | +```output |
| 164 | +null |
| 165 | +``` |
| 166 | + |
| 167 | +8. Measure Execution Time (Optional): |
| 168 | + |
| 169 | +The below snippet measures how long it takes to insert documents for performance insight. |
| 170 | +```javascript |
| 171 | +var start = new Date() |
| 172 | +for (let i = 0; i < 10000; i++) { |
| 173 | + db.test.insertOne({ sample: i }) |
| 174 | +} |
| 175 | +print("Insert duration (ms):", new Date() - start) |
| 176 | +``` |
| 177 | +You should see an output similar to: |
| 178 | + |
| 179 | +```output |
| 180 | +Insert duration (ms): 4427 |
| 181 | +``` |
| 182 | +9. Count Total Documents: |
| 183 | + |
| 184 | +Count total entries to confirm expected data volume. |
| 185 | +```javascript |
| 186 | +db.test.countDocuments() |
| 187 | +``` |
| 188 | +You should see an output similar to: |
| 189 | + |
| 190 | +```output |
| 191 | +19999 |
| 192 | +``` |
| 193 | +The count **19999** reflects the total documents after inserting 10,000 initial records, adding 10,000 more (in point 8), and deleting one (record: 100). |
| 194 | + |
| 195 | +10. Clean Up (Optional): |
| 196 | + |
| 197 | +Deletes the **baselineDB** database and all its contents. |
| 198 | +```javascript |
| 199 | +db.dropDatabase() |
| 200 | +``` |
| 201 | +You should see an output similar to: |
| 202 | + |
| 203 | +```output |
| 204 | +{ ok: 1, dropped: 'baselineDB' } |
| 205 | +``` |
| 206 | + |
| 207 | +The above is a destructive command that completely deletes the current database you are connected to in mongosh. |
| 208 | + |
| 209 | +The above operations confirm that MongoDB is installed successfully and is functioning as expected on the GCP Arm64 environment. |
| 210 | + |
| 211 | +Using **mongosh**, you validated key database operations such as **insert**, **read**, **update**, **delete**, and **count**. |
| 212 | +Now, your MongoDB instance is ready for further benchmarking and production use. |
0 commit comments