How to use Mongoose with Deno
Mongoose is a popular, schema-based library that models data for MongoDB. It simplifies writing MongoDB validation, casting, and other relevant business logic.
This tutorial will show you how to setup Mongoose and MongoDB with your Deno project.
View source or check out the video guide.
Creating a Mongoose Model
Let’s create a simple app that connects to MongoDB, creates a Dinosaur
model,
and adds and updates a dinosaur to the database.
First, we’ll create the necessary files and directories:
$ touch main.ts && mkdir model && touch model/Dinosaur.ts
In /model/Dinosaur.ts
, we’ll import npm:mongoose
, define the [schema], and
export it:
import { model, Schema } from "npm:mongoose@^6.7";
// Define schema.
const dinosaurSchema = new Schema({
name: { type: String, unique: true },
description: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
});
// Validations
dinosaurSchema.path("name").required(true, "Dinosaur name cannot be blank.");
dinosaurSchema.path("description").required(
true,
"Dinosaur description cannot be blank.",
);
// Export model.
export default model("Dinosaur", dinosaurSchema);
Connecting to MongoDB
Now, in our main.ts
file, we’ll import mongoose and the Dinosaur
schema, and
connect to MongoDB:
import mongoose from "npm:mongoose@^6.7";
import Dinosaur from "./model/Dinosaur.ts";
await mongoose.connect("mongodb://localhost:27017");
// Check to see connection status.
console.log(mongoose.connection.readyState);
Because Deno supports top-level await
, we’re able to simply
await mongoose.connect()
.
Running this, we should expect a log of 1
:
$ deno run --allow-read --allow-sys --allow-env --allow-net main.ts
1
It worked!
Manipulating Data
Let’s add an instance method
to our Dinosaur
schema in /model/Dinosaur.ts
:
// ./model/Dinosaur.ts
// Methods.
dinosaurSchema.methods = {
// Update description.
updateDescription: async function (description: string) {
this.description = description;
return await this.save();
},
};
// ...
This instance method, updateDescription
, will allow you to update a record’s
description.
Back in main.ts
, let’s start adding and manipulating data in MongoDB.
// main.ts
// Create a new Dinosaur.
const deno = new Dinosaur({
name: "Deno",
description: "The fastest dinosaur ever lived.",
});
// // Insert deno.
await deno.save();
// Find Deno by name.
const denoFromMongoDb = await Dinosaur.findOne({ name: "Deno" });
console.log(
`Finding Deno in MongoDB -- \n <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>d</mi><mi>e</mi><mi>n</mi><mi>o</mi><mi>F</mi><mi>r</mi><mi>o</mi><mi>m</mi><mi>M</mi><mi>o</mi><mi>n</mi><mi>g</mi><mi>o</mi><mi>D</mi><mi>b</mi><mi mathvariant="normal">.</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mo>:</mo></mrow><annotation encoding="application/x-tex">{denoFromMongoDb.name}: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">m</span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal" style="margin-right:0.02778em;">oD</span><span class="mord mathnormal">b</span><span class="mord">.</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>{denoFromMongoDb.description}`,
);
// Update description for Deno and save it.
await denoFromMongoDb.updateDescription(
"The fastest and most secure dinosaur ever lived.",
);
// Check MongoDB to see Deno's updated description.
const newDenoFromMongoDb = await Dinosaur.findOne({ name: "Deno" });
console.log(
`Finding Deno (again) -- \n <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>n</mi><mi>e</mi><mi>w</mi><mi>D</mi><mi>e</mi><mi>n</mi><mi>o</mi><mi>F</mi><mi>r</mi><mi>o</mi><mi>m</mi><mi>M</mi><mi>o</mi><mi>n</mi><mi>g</mi><mi>o</mi><mi>D</mi><mi>b</mi><mi mathvariant="normal">.</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mo>:</mo></mrow><annotation encoding="application/x-tex">{newDenoFromMongoDb.name}: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">n</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">De</span><span class="mord mathnormal">n</span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">m</span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal" style="margin-right:0.02778em;">oD</span><span class="mord mathnormal">b</span><span class="mord">.</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>{newDenoFromMongoDb.description}`,
);
Running the code, we get:
Finding Deno in MongoDB --
Deno: The fastest dinosaur ever lived.
Finding Deno (again) --
Deno: The fastest and most secure dinosaur ever lived.
Boom!
For more info on using Mongoose, please refer to their documentation.