Support of MongoDB for Node.js

CAST supports MongoDB via its com.castsoftware.nodejsexternal link extension. Details about the support provided for Node.js source code is discussed below.

Supported Libraries

The following libraries are supported:

  • MongoDB
  • Mongoose
  • Prisma

Objects

Icon Description
Node.js MongoDB connection
Node.js unknown MongoDB connection
Node.js 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
  • db.collection.insertMany
useUpdateLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • db.collection.findOneAndUpdate
  • db.collection.findOneAndReplace
  • db.collection.replaceOne
  • db.collection.updateOne
  • db.collection.updateMany
useDeleteLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • db.collection.findOneAndDelete
  • db.collection.deleteOne
  • db.collection.deleteMany
  • db.collection.remove
useSelectLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • db.collection.find
  • db.collection.findOne

Example

Taking the following codes:

var MongoClient = require('mongodb').MongoClient

var url = 'mongodb://localhost:27017/myproject';

var insertDocuments = function(db, callback) {
  var collection = db.collection('documents');
  collection.insertMany([
    {a : 1}, {a : 2}, {a : 3}
  ], function(err, result) {
    callback(result);
  });
}

var updateDocument = function(db, callback) {
  var collection = db.collection('documents');
  for (i = 0; i < 2; i += 1) {
	collection.updateOne({ a : 2 }
	  , { $set: { b : 1 } }, function(err, result) {
	  callback(result);
	});
  }
}

MongoClient.connect(url, function(err, db) {
  insertDocuments(db, function() {
    updateDocument(db, function() {
      db.close();
    });
  });
});

In this example, a ‘Node.js MongoDB connection’ and a ‘Node.js MongoDB collection’ objects are created. This extension creates a ‘useInsert’ link from function ‘insertDocuments’ to the collection ‘documents’ and a ‘useUpdate’ link from function ‘updateDocument’ to the collection ‘documents’:

Supported Mongoose

Link Type Source and destination of link Supported APIs
useInsertLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • model.create
  • model.insertMany
useUpdateLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • model.findByIdAndUpdate
  • model.findOneAndUpdate
  • model.findOneAndReplace
  • model.replaceOne
  • model.updateOne
  • model.updateMany
useDeleteLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • model.deleteOne
  • model.deleteMany
  • model.findByIdAndDelete
  • model.findOneAndDelete
useSelectLink Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection
  • model.exists
  • model.find
  • model.findOne
  • model.findById
  • model.where

Example

Taking the following codes:

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/analyzerlauncher', function(err) {
  if (err) { throw err; }
});

userModel = mongoose.model('users', userSchema);

function find(req,res) {
  userModel.findOne(req.params.id, function (err, authorize) {})
}

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 ‘find’ to the collection ‘users’:

Supported Prisma

See Prisma support for Node.js - MongoDB database.