Support of MongoDB for Java

CAST supports MongoDB via its com.castsoftware.nosqljava 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)

Mongo Jack Up to: 4.11.0

(tick)

Mongo Java Sync Driver Up to: 5.0.0

(tick)

Morphia Up to: 1.3.2

(tick)

Supported Operations

Operation

Methods supported

Insert

com.mongodb.DBCollection.insert

com.mongodb.client.MongoCollection.insertOne

com.mongodb.client.MongoCollection.insertMany

com.mongodb.client.MongoCollection.bulkWrite

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.client.MongoCollection.bulkWrite

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

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

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

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

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

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

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.