Support of MongoDB for Java

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

Supported Libraries

Library Version Supported
Jongo  Up to: 1.4.0

(tick)

Mongo Java Driver  Up to: 3.12.8

(tick)

MongoJack Up to: 4.11.0

(tick)

Mongo Java Sync Driver Up to: 5.0.0

(tick)

Mongo Morphia Up to: 1.3.2

(tick)

Mongo Java Async Driver Up to: 3.12.14

(tick)

Mongo Reactive Streams Driver Up to: 5.0.0

(tick)

Supported Operations

Operation

Methods supported

Insert

com.mongodb.DBCollection.insert

com.mongodb.client.MongoCollection.insertOne

com.mongodb.client.MongoCollection.insertMany

com.mongodb.DBCollection.save

org.jongo.MongoCollection.insert

org.jongo.MongoCollection.save

org.mongojack.JacksonMongoCollection.insert

org.mongojack.MongoCollectionDecorator.insertMany

org.mongojack.MongoCollectionDecorator.insertOne

com.mongodb.DBCollection.save

org.jongo.MongoCollection.insert

org.jongo.MongoCollection.save

com.mongodb.DBCollection.replaceOrInsert

com.mongodb.DBCollection.setHintFields

com.mongodb.DBCollection.executeWriteOperation

com.mongodb.DBCollection.executeBulkWriteOperation

com.mongodb.async.client.MongoCollection.insertMany

com.mongodb.async.client.MongoCollection.createIndex

com.mongodb.async.client.MongoCollection.createIndexes

com.mongodb.async.client.MongoCollection.insertOne

org.mongodb.morphia.Datastore.save

org.mongodb.morphia.dao.BasicDAO.save

org.mongodb.morphia.dao.DAO.save

Select

com.mongodb.client.MongoDatabase.getCollection

com.mongodb.client.MongoCollection.countDocuments

com.mongodb.client.MongoCollection.count

com.mongodb.client.MongoCollection.find

com.mongodb.client.MongoCollection.findOneAndDelete

com.mongodb.client.MongoCollection.findOneAndReplace

com.mongodb.client.MongoCollection.findOneAndUpdate

com.mongodb.DB.getCollection

com.mongodb.DBCollection.aggregate

com.mongodb.DBCollection.count

com.mongodb.DBCollection.distinct

com.mongodb.DBCollection.find

com.mongodb.DBCollection.findOne

com.mongodb.DBCollection.getCollection

com.mongodb.DBCollection.getCount

com.mongodb.DBCollection.getName

com.mongodb.DBCollection.group

com.mongodb.DBCollection.findAndModify

com.mongodb.DBCollection.findAndRemove

org.jongo.Jongo.getCollection

org.jongo.MongoCollection.aggregate

org.jongo.MongoCollection.count

org.jongo.MongoCollection.distinct

org.jongo.MongoCollection.find

org.jongo.MongoCollection.findAndModify

org.jongo.MongoCollection.findOne

org.jongo.MongoCollection.getDBCollection

org.jongo.MongoCollection.getName

org.mongojack.JacksonMongoCollection.findOne

org.mongojack.JacksonMongoCollection.findOneById

org.mongojack.JacksonMongoCollection.getCollectionKey

org.mongojack.JacksonMongoCollection.getName

org.mongojack.JacksonMongoCollection.getMongoCollection

org.mongojack.MongoCollectionDecorator.countDocuments

org.mongojack.MongoCollectionDecorator.estimatedDocumentCount

org.mongojack.MongoCollectionDecorator.find

org.mongojack.MongoCollectionDecorator.findOneAndDelete

org.mongojack.MongoCollectionDecorator.findOneAndReplace

org.mongojack.MongoCollectionDecorator.findOneAndUpdate

com.mongodb.DBCollection.getFullName

com.mongodb.DBCollection.getHintFields

com.mongodb.DBCollection.getDefaultDBObjectCodec

com.mongodb.client.MongoCollection.aggregate

com.mongodb.client.MongoCollection.watch

org.mongodb.morphia.Datastore.get

org.mongodb.morphia.Datastore.find

org.mongodb.morphia.Datastore.getByKey

org.mongodb.morphia.Datastore.getCollection

org.mongodb.morphia.Datastore.getCount

org.mongodb.morphia.Datastore.findAndDelete

org.mongodb.morphia.Datastore.findAndModify

org.mongodb.morphia.Datastore.getKey

org.mongodb.morphia.Datastore.merge

org.mongodb.morphia.dao.BasicDAO.count

org.mongodb.morphia.dao.BasicDAO.ensureIndexes

org.mongodb.morphia.dao.BasicDAO.exists

org.mongodb.morphia.dao.BasicDAO.findIds

org.mongodb.morphia.dao.BasicDAO.findOne

org.mongodb.morphia.dao.BasicDAO.findOneId

org.mongodb.morphia.dao.BasicDAO.get

org.mongodb.morphia.dao.DAO.count

org.mongodb.morphia.dao.DAO.exists

org.mongodb.morphia.dao.DAO.find

org.mongodb.morphia.dao.DAO.findIds

org.mongodb.morphia.dao.DAO.findOne

org.mongodb.morphia.dao.DAO.findOneId

org.mongodb.morphia.dao.DAO.get

org.mongodb.morphia.query.QueryResults.asKeyList

org.mongodb.morphia.query.QueryResults.asList

org.mongodb.morphia.query.QueryResults.countAll

org.mongodb.morphia.query.QueryResults.count

org.mongodb.morphia.query.QueryResults.fetch

org.mongodb.morphia.query.QueryResults.fetchEmptyEntities

org.mongodb.morphia.query.QueryResults.fetchKeys

org.mongodb.morphia.query.QueryResults.get

org.mongodb.morphia.query.QueryResults.getKey

org.mongodb.morphia.query.QueryResults.tail

com.mongodb.async.client.MongoCollection.count

com.mongodb.async.client.MongoCollection.find

com.mongodb.async.client.MongoCollection.countDocuments

com.mongodb.async.client.MongoCollection.estimatedDocumentCount

com.mongodb.async.client.MongoCollection.distinct

com.mongodb.async.client.MongoCollection.FindIterable

com.mongodb.async.client.MongoCollection.AggregateIterable

com.mongodb.async.client.MongoCollection.watch

com.mongodb.async.client.MongoCollection.mapReduce

com.mongodb.async.client.MongoCollection.listIndexes

Delete

com.mongodb.client.MongoCollection.drop

com.mongodb.client.MongoCollection.deleteOne

com.mongodb.client.MongoCollection.deleteMany

com.mongodb.client.MongoCollection.findOneAndDelete

com.mongodb.DBCollection.drop

com.mongodb.DBCollection.dropIndex

com.mongodb.DBCollection.findAndRemove

org.jongo.MongoCollection.drop

org.jongo.MongoCollection.dropIndex

org.jongo.MongoCollection.dropIndexes

org.jongo.MongoCollection.remove

org.mongojack.JacksonMongoCollection.removeById

org.mongojack.JacksonMongoCollection.drop

org.mongojack.MongoCollectionDecorator.deleteMany

org.mongojack.MongoCollectionDecorator.deleteOne

org.mongojack.MongoCollectionDecorator.drop

org.mongojack.MongoCollectionDecorator.dropIndex

org.mongojack.MongoCollectionDecorator.dropIndexes

org.mongojack.MongoCollectionDecorator.findOneAndDelete

com.mongodb.async.client.MongoCollection.deleteOne

com.mongodb.async.client.MongoCollection.deleteMany

com.mongodb.async.client.MongoCollection.findOneAndDelete

com.mongodb.async.client.MongoCollection.drop

com.mongodb.async.client.MongoCollection.dropIndex

com.mongodb.async.client.MongoCollection.dropIndexes

org.mongodb.morphia.Datastore.delete

org.mongodb.morphia.dao.BasicDAO.delete

org.mongodb.morphia.dao.BasicDAO.deleteById

org.mongodb.morphia.dao.BasicDAO.deleteByQuery

org.mongodb.morphia.dao.DAO.delete

org.mongodb.morphia.dao.DAO.deleteById

org.mongodb.morphia.dao.DAO.deleteByQuery

Update

com.mongodb.client.MongoCollection.updateOne

com.mongodb.client.MongoCollection.updateMany

com.mongodb.client.MongoCollection.findOneAndReplace

com.mongodb.client.MongoCollection.findOneAndUpdate

org.jongo.MongoCollection.update

com.mongodb.DBCollection.update

com.mongodb.DBCollection.updateMulti

com.mongodb.DBCollection.findAndModify

com.mongodb.client.MongoCollection.replaceOne

org.mongojack.JacksonMongoCollection.replaceOne

org.mongojack.MongoCollectionDecorator.findOneAndReplace

org.mongojack.MongoCollectionDecorator.findOneAndUpdate

org.mongojack.MongoCollectionDecorator.renameCollection

org.mongojack.MongoCollectionDecorator.replaceOne

org.mongojack.MongoCollectionDecorator.updateMany

org.mongojack.MongoCollectionDecorator.updateOne

com.mongodb.client.MongoCollection.renameCollection

com.mongodb.async.client.MongoCollection.renameCollection

com.mongodb.async.client.MongoCollection.replaceOne

com.mongodb.async.client.MongoCollection.updateOne

com.mongodb.async.client.MongoCollection.updateMany

com.mongodb.async.client.MongoCollection.findOneAndReplace

com.mongodb.async.client.MongoCollection.findOneAndUpdate

org.mongodb.morphia.Datastore.update

org.mongodb.morphia.Datastore.updateFirst

org.mongodb.morphia.dao.BasicDAO.UpdateOperations

org.mongodb.morphia.dao.BasicDAO.update

org.mongodb.morphia.dao.BasicDAO.updateFirst

org.mongodb.morphia.dao.DAO.update

org.mongodb.morphia.dao.DAO.updateFirst

use

org.reactivestreams.Publisher.subscribe

com.mongodb.async.client.MongoCollection.bulkWrite

com.mongodb.client.MongoCollection.bulkWrite

com.mongodb.BulkWriteOperation.execute

Objects

Icon Description

Java MongoDB database

Java MongoDB collection

Java unknown MongoDB database

Java unknown MongoDB collection
Link type Source and destination of link Methods supported
belongsTo

From Java MongoDB collection object to Java MongoDB database object

-
useLink Between the caller Java Method objects (constructors also) and Java Mongo Collection Object

mapReduce

createCollection

subscribe

bulkWrite

execute

useSelectLink

Between the caller Java Method objects (constructors also) and Java Mongo Collection Object

getCollection

countDocuments

find 

count

findOneAndDelete

findOneandReplace

findOneAndUpdate

aggregate

distinct

findOne

getCount

getName

group

findAndModify

findAndRemove

useUpdateLink Between the caller Java Method objects (constructors also) and Java Mongo Collection Object

update

updateOne

updateMany

findOneAndUpdate

fineOneAndReplace

findAndModify

rename

replaceOne

useDeleteLink Between the caller Java Method objects (constructors also) and Java Mongo Collection Object

findOneAndDelete

drop

deleteOne

deleteMany

dropIndex

dropIndexes

remove

findAndRemove

useInsertLink Between the caller Java Method objects (constructors also) and Java Mongo Collection Object

insert

insertOne

insertMany

bulkWrite

save

Amazon DocumentDB Identification

The below mentioned objects have properties to indicate whether the object is of type MongoDB or Amazon DocumentDB:

Java MongoDB collection object

Property Description

Type of Property Value

Value: Meaning

Is an Amazon DocumentDB collection? Integer

1: The collection object is of type Amazon DocumentDB

0: The collection object is of type MongoDB

Java unknown MongoDB collection object

Property Description

Type of Property Value

Value: Meaning

Is an Amazon DocumentDB collection? Integer

1: The collection object is of type Amazon DocumentDB

0: The collection object is of type MongoDB

What results can you expect?

Some example scenarios are shown below:

Creation of Database and Collection 

public MongoUserDAO(DB db) {
        Mongo mongo = new Mongo();
        db = mongo.getDB("TestDatabase");
        db.createCollection("User", dbo);
}

Insert Operation

public void insertDocuments(DBObject[] documents) {
        userCol.insert(documents);  
    }

Select Operation

public void getCollection() {
        DBCollection col =  db.getCollection("User");
    }

Update Operation

public void updateDoc(DBObject query, DBObject update) {
        userCol.update(query, update);
    }

Delete Operation

Amazon DocumentDB Identification

package com.sample.app;
 
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
 
 
public final class App {
    private App() {
    }
    public static void main(String[] args) {
 
        String connectionString = "mongodb://root:rootroot@sample-cluster.cluster-cqjaxx9hbhi0.us-west-2.docdb.amazonaws.com:27017/?replicaSet=rs0&readpreference=secondaryPreferred";
 
        MongoClientURI clientURI = new MongoClientURI(connectionString);
        MongoClient mongoClient = new MongoClient(clientURI);
 
        MongoDatabase testDB = mongoClient.getDatabase("library");
        MongoCollection<Document> numbersCollection = testDB.getCollection("books");
 
        Document doc = new Document("name", "title").append("value", "Harry Potter");
        numbersCollection.insertOne(doc);
 
        MongoCursor<Document> cursor = numbersCollection.find().iterator();
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next().toJson());
            }
        } finally {
            cursor.close();
        }
 
    }
}

Known Limitations

  • For the method ‘mapReduce’ the type of link produced is ‘useLink’ as its type is not determined
  • If collection name is not resolved in the CRUD API, then link is created with unknown collection object.
  • Setting of properties related to Amazon DocumentDB identification depends on proper resolution of connection string.