DEV Community

Cover image for MongoDB database driver for Deno
Keramot UL Islam
Keramot UL Islam

Posted on

3 1

MongoDB database driver for Deno

As you already know that Deno is a new run time of JavaScript and TypeScript. Recently it has published first stable release.

MongoDB is the most popular NoSQL database. Deno also has a third-party module deno_mongo to handle MongoDB queries.

deno_mongo is a MongoDB database driver for Deno, based on rust's official MongoDB library package. Right now it's on --unstable flug.

I'm assuming that you have installed MongoDB and Deno on your machine. So, let's see how it works...
Necessary permissions to run this module: --allow-net --allow-write --allow-read --allow-plugin --unstable

import { MongoClient } from "https://deno.land/x/mongo/mod.ts"

const client = new MongoClient()
client.connectWithUri("mongodb://localhost:27017")

const db = client.database("denoDB");
const greetings = db.collection("greetings");
Enter fullscreen mode Exit fullscreen mode

on client.connectWithUri you will put your own URL. In my case, I have a database called denoDB and a collection called greetings in that database.

Now let's insert some data in there:

const hello = await greetings.insertOne({
    sayHello: "Hello World..."
});
Enter fullscreen mode Exit fullscreen mode

If you check on the terminal db.greetings.find() then you'll get that data back, or you can find the data using deno_mongo:

const find = await greetings.find({
    _id: hello
});
console.log(find)
Enter fullscreen mode Exit fullscreen mode



Update and Delete:
updateOne returns { matchedCount, modifiedCount, upsertedId } these three informations.

// update
const { matchedCount, modifiedCount, upsertedId } = await greetings.updateOne(
    { sayHello: { $ne: null } },
    { $set: { sayHello: "Hello Universe..." } }
);

// delete
const delete = await greetings.deleteOne({ _id: hello });
Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay