Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced16 min

Connecting Express to MongoDB

Understand the idea

One route reads a collection and sends the titles back as JSON.

The idea

The middleware connects once, seeds a Welcome , and calls next. The GET /notes reads the and sends JSON. The database exists only for this run.

What changes

From request to documents
PieceFromJob
GET /notesThe HTTP cycle and Express routingMatch one request
MongoClientThe collection lessonReach a fresh database
res.jsonThe response lessonSend the titles once

The command

GET /notes

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

The mistake

Check the result

Status 200 and a JSON array containing Welcome. A second Run inserts Welcome again into a new database. It does not append to the first one.

Guided challenge

Seed Welcome and return it

The route seeds Hello into the collection. Change the seed title to Welcome so GET /notes returns that title in the JSON array.

Seed Welcome and return it

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

status 200
["Welcome"]

Hints 0/2

    Use all hints or make 3 failed checks (0/3).

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does GET /notes return?
    02Which earlier lessons does this combine?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson