Support of MongoDB for Node.js
Supported Libraries
The following libraries are supported:
- MongoDB
- Mongoose
- Prisma
Objects
| Icon | Description |
|---|---|
| NodeJS MongoDB database | |
| NodeJS Unknown MongoDB database | |
| NodeJS MongoDB collection | |
| NodeJS Unknown MongoDB collection |
Supported MongoDB
Supported links
| Link Type | Source and destination of link | Supported APIs |
|---|---|---|
| useInsertLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
| useUpdateLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
| useDeleteLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
| useSelectLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
Example
Taking the following codes:
const { MongoClient } = require("mongodb");
const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);
async function my_add_object() {
const db = client.db("testdb");
const foo = db.collection("FooCollection");
// Insert a single object
const result = await foo.insertOne({ name: "Alice", age: 25, active: true });
console.log("Inserted object:", result.insertedId);
}
async function my_bulkwrite() {
const db = client.db("testdb");
const foo = db.collection("FooCollection");
// Perform multiple operations in bulk
const result = await foo.bulkWrite([
{ insertOne: { document: { name: "Bob", age: 30, active: false } } },
{ updateOne: { filter: { name: "Alice" }, update: { $set: { active: false } } } },
{ deleteOne: { filter: { name: "Charlie" } } }
]);
console.log("BulkWrite result:", result);
}
async function my_aggregate() {
const db = client.db("testdb");
const foo = db.collection("FooCollection");
// Simple aggregation: group by 'active' and count
const pipeline = [
{ $group: { _id: "$active", count: { $sum: 1 } } },
{ $merge: { into: "TargetCollection", whenMatched: "merge", whenNotMatched: "insert" } }
];
const result = await foo.aggregate(pipeline).toArray();
console.log("Aggregation result (before merge):", result);
// Check TargetCollection contents
const target = db.collection("TargetCollection");
const merged = await target.find().toArray();
console.log("TargetCollection after merge:", merged);
}
In this example, a ‘Node.js MongoDB database’ and a ‘NodeJS MongoDB collection’ objects are created. This extension creates a ‘useInsert’ link from function ‘my_add_object’ to the collection ‘FooCollection’. It also creates a ‘useInsert’, a ‘useUpdate’ and a useDelete link from ‘my_bulkwrite’ function to the ‘FooCollection’. Finally, it creates a ‘useSelect’ link from the ‘my_aggregate’ function to the ‘FooCollection’ as well as a ‘useInsert’ and ‘useUpdate’ link to the ‘TargetCollection’ specified in the $merge stage of the aggregation pipeline.

Supported Mongoose
Supported links
| Link Type | Source and destination of link | Supported APIs |
|---|---|---|
| useInsertLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
| useUpdateLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
| useDeleteLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
| useSelectLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
Example
Taking the following codes:
// foo-mongoose-example.js
const mongoose = require("mongoose");
const uri = "mongodb://localhost:27017/testdb";
// Define schema & model
const fooSchema = new mongoose.Schema({
name: String,
age: Number,
active: Boolean,
});
const FooCollection = mongoose.model("FooCollection", fooSchema);
async function my_add_object() {
// Insert a single object using the model
const doc = new FooCollection({ name: "Alice", age: 25, active: true });
await doc.save();
console.log("Inserted object:", doc._id);
}
async function my_bulkwrite() {
// Use the model's bulkWrite API
const result = await FooCollection.bulkWrite([
{ insertOne: { document: { name: "Bob", age: 30, active: false } } },
{ updateOne: { filter: { name: "Alice" }, update: { $set: { active: false } } } },
{ deleteOne: { filter: { name: "Charlie" } } },
]);
console.log("BulkWrite result:", result);
}
async function my_aggregate() {
// Simple aggregation: group by 'active' and count
const pipeline = [
{ $group: { _id: "$active", count: { $sum: 1 } } },
{
$merge: {
into: "TargetCollection",
whenMatched: "merge",
whenNotMatched: "insert",
},
},
];
const result = await FooCollection.aggregate(pipeline);
console.log("Aggregation result (before merge):", result);
// Check TargetCollection contents (via mongoose.connection.db)
const merged = await mongoose.connection.db
.collection("TargetCollection")
.find()
.toArray();
console.log("TargetCollection after merge:", merged);
}
async function main() {
try {
await mongoose.connect(uri);
await my_add_object();
await my_bulkwrite();
await my_aggregate();
} finally {
await mongoose.disconnect();
}
}
main().catch(console.error);
In this example, a ‘Node.js MongoDB database’ and a ‘NodeJS MongoDB collection’ objects are created. This extension creates a ‘useInsert’ link from function ‘my_add_object’ to the collection ‘FooCollection’. It also creates a ‘useInsert’, a ‘useUpdate’ and a useDelete link from ‘my_bulkwrite’ function to the ‘FooCollection’. Finally, it creates a ‘useSelect’ link from the ‘my_aggregate’ function to the ‘FooCollection’ as well as a ‘useInsert’ and ‘useUpdate’ link to the ‘TargetCollection’ specified in the $merge stage of the aggregation pipeline..

Supported Prisma
See Prisma support for Node.js - MongoDB database.