📖 Scenario: You are building a simple user management system using Deno and MongoDB. You want to store user information such as name and email in a MongoDB collection.
🎯 Goal: Build a Deno script that connects to MongoDB, creates a users collection, inserts user data, and queries the collection.
📋 What You'll Learn
Create a MongoDB client connection using Deno.
Define a database and a collection named users.
Insert multiple user documents with name and email fields.
Query the users collection to find users with a specific email domain.
💡 Why This Matters
🌍 Real World
Storing and querying user data is common in web applications, such as user profiles or contact lists.
💼 Career
Understanding how to connect Deno with MongoDB and perform basic CRUD operations is valuable for backend development roles.
Progress0 / 4 steps
1
Setup MongoDB Client Connection
Import MongoClient from "https://deno.land/x/mongo@0.31.1/mod.ts" and create a new MongoClient instance called client. Connect client to MongoDB at "mongodb://127.0.0.1:27017".
Deno
Hint
Use new MongoClient() to create the client and await client.connect() to connect.
2
Define Database and Collection
Create a variable called db that selects the database named "userdb" from client. Then create a variable called users that selects the collection named "users" from db.
Deno
Hint
Use client.database("userdb") and then db.collection("users").
3
Insert Multiple User Documents
Insert these three user documents into the users collection using insertMany: { name: "Alice", email: "alice@example.com" }, { name: "Bob", email: "bob@example.com" }, and { name: "Carol", email: "carol@test.com" }.
Deno
Hint
Use await users.insertMany([...]) with the exact user objects.
4
Query Users by Email Domain
Create a variable called exampleUsers that finds all users in users collection whose email ends with "@example.com" using find with a regular expression filter.
Deno
Hint
Use users.find({ email: { $regex: /@example\.com$/ } }) and convert to array with toArray().
Practice
(1/5)
1. What is the primary way MongoDB stores data when used with Deno?
easy
A. In collections of documents
B. In tables with rows and columns
C. As key-value pairs in a flat file
D. In XML files
Solution
Step 1: Understand MongoDB data structure
MongoDB stores data in collections, which are groups of documents.
Step 2: Compare with other storage types
Unlike SQL databases, MongoDB does not use tables but uses collections of JSON-like documents.
Final Answer:
In collections of documents -> Option A
Quick Check:
MongoDB = collections of documents [OK]
Hint: Remember MongoDB uses collections, not tables [OK]
Common Mistakes:
Confusing MongoDB with SQL tables
Thinking MongoDB stores data in flat files
Assuming XML storage
2. Which of the following is the correct way to create a MongoClient instance in Deno?
easy
A. const client = new mongo.Client();
B. const client = MongoClient();
C. const client = mongoClient();
D. const client = new MongoClient();
Solution
Step 1: Check MongoClient instantiation syntax
In Deno, you create a new MongoClient instance using the 'new' keyword and the exact class name 'MongoClient'.
Step 2: Validate option correctness
const client = new MongoClient(); uses 'new MongoClient()', which is correct. Other options have incorrect casing or missing 'new'.
Final Answer:
const client = new MongoClient(); -> Option D
Quick Check:
Use 'new MongoClient()' to create client [OK]
Hint: Use 'new' keyword with exact class name MongoClient [OK]
Common Mistakes:
Omitting 'new' keyword
Wrong casing in 'MongoClient'
Using incorrect module or variable names
3. Given the following Deno MongoDB code snippet, what will be the output?