Support of MongoDB for TypeScript


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

Link Type Source and destination of link Supported APIs
useInsertLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • mongodb.Collection.insertMany
  • mongodb.Collection.insertOne
  • mongodb.Collection.insert
  • mongodb.Collection.bulkWrite (depending on the argument passed)
  • mongodb.Collection.aggregate (to the target given in $out or $merge)
useUpdateLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • mongodb.Collection.replaceOne
  • mongodb.Collection.updateMany
  • mongodb.Collection.updateOne
  • mongodb.Collection.findOneAndReplace
  • mongodb.Collection.findOneAndUpdate
  • mongodb.Collection.update
  • mongodb.Collection.bulkWrite (depending on the argument passed)
  • mongodb.Collection.aggregate (to the target given in $out or $merge)
useDeleteLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • mongodb.Collection.deleteOne
  • mongodb.Collection.deleteMany
  • mongodb.Collection.drop
  • mongodb.Collection.findOneAndDelete
  • mongodb.Collection.remove
  • mongodb.Collection.bulkWrite (depending on the argument passed)
useSelectLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • mongodb.Collection.aggregate
  • mongodb.Collection.findOne
  • mongodb.Collection.findOneAndDelete
  • mongodb.Collection.findOneAndReplace
  • mongodb.Collection.findOneAndUpdate
  • mongodb.Collection.find
  • mongodb.Collection.count
  • mongodb.Collection.countDocuments
  • mongodb.Collection.dataSize
  • mongodb.Collection.distinct
  • mongodb.Collection.estimatedDocumentCount
  • mongodb.Collection.findAndModify
  • mongodb.Collection.bulkWrite (depending on the argument passed)

Example

Taking the following codes:

import { MongoClient, Db, Collection, Document, BulkWriteResult } from "mongodb";

const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);

async function my_add_object(): Promise<void> {
  const db: Db = client.db("testdb");
  const foo: Collection<Document> = 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(): Promise<void> {
  const db: Db = client.db("testdb");
  const foo: Collection<Document> = db.collection("FooCollection");

  // Perform multiple operations in bulk
  const result: BulkWriteResult = 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(): Promise<void> {
  const db: Db = client.db("testdb");
  const foo: Collection<Document> = db.collection("FooCollection");

  // Simple aggregation: group by 'active' and count, then merge results into another collection
  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: Collection<Document> = db.collection("TargetCollection");
  const merged = await target.find().toArray();
  console.log("TargetCollection after merge:", merged);
}

async function main(): Promise<void> {
  try {
    await client.connect();
    await my_add_object();
    await my_bulkwrite();
    await my_aggregate();
  } catch (err) {
    console.error("Error:", err);
  } finally {
    await client.close();
  }
}

main().catch(console.error);

In this example, a ‘Node.js MongoDB connection’ and a ‘Node.js MongoDB collection’ objects are created. This extension creates a ‘useSelect’ link from function ‘Anonymous1’ to the collection ‘docs’:

Supported Mongoose

Link Type Source and destination of link Supported APIs
useInsertLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • mongoose.Model.bulkSave
  • mongoose.Model.create
  • mongoose.Model.findOneAndReplace
  • mongoose.Model.insertMany
  • mongoose.Model.insertOne
  • mongoose.Model.save
  • mongoose.Model.bulkWrite (depending on the argument passed)
  • mongoose.Model.aggregate (to the target given in $out or $merge)
useUpdateLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • mongoose.Model.bulkSave
  • mongoose.Model.findByIdAndUpdate
  • mongoose.Model.findOneAndUpdate
  • mongoose.Model.updateOne
  • mongoose.Model.updateMany
  • mongoose.Model.save
  • mongoose.Model.update
  • mongoose.Model.replaceOne
  • mongoose.Model.bulkWrite (depending on the argument passed)
  • mongoose.Model.aggregate (to the target given in $out or $merge)
useDeleteLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • mongoose.Model.deleteMany
  • mongoose.Model.deleteOne
  • mongoose.Model.findByIdAndDelete
  • mongoose.Model.findOneAndReplace
  • mongoose.Model.remove
  • mongoose.Model.findByIdAndRemove
  • mongoose.Model.findOneAndRemove
  • mongoose.Model.bulkWrite (depending on the argument passed)
useSelectLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • mongoose.Model.aggregate
  • mongoose.Model.find
  • mongoose.Model.findById
  • mongoose.Model.findByIdAndDelete
  • mongoose.Model.findByIdAndUpdate
  • mongoose.Model.findOne
  • mongoose.Model.findOneAndDelete
  • mongoose.Model.findOneAndReplace
  • mongoose.Model.findOneAndUpdate
  • mongoose.Model.countDocuments
  • mongoose.Model.estimatedDocumentCount
  • mongoose.Model.exists
  • mongoose.Model.where
  • mongoose.Model.bulkWrite (depending on the argument passed)

Example

Taking the following codes:

// foo-mongoose-example.ts
import { Schema, model, connect, disconnect }  from 'mongoose;

const uri = "mongodb://localhost:27017/testdb";

// Define schema & model
const fooSchema = new Schema({
  name: String,
  age: Number,
  active: Boolean,
});
const FooCollection = 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);
}

async function main() {
  try {
    await connect(uri);

    await my_add_object();
    await my_bulkwrite();
    await my_aggregate();
  } finally {
    await disconnect();
  }
}

main().catch(console.error);

In this example, a ‘Node.js MongoDB connection’ and a ‘Node.js MongoDB collection’ objects are created. This extension creates a ‘useSelect’ link from method ‘findbyId’ to the collection ‘Engagement’:

Supported Prisma

See Node.js - Prisma support - MongoDB database.