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

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:

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

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.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.