This documentation is not maintained. Please refer to doc.castsoftware.com/technologies to find the latest updates.

CAST supports MongoDB via its com.castsoftware.nosqldotnet extension. As Amazon DocumentDB is MongoDB compatible, the support also applies to Amazon DocumentDB. Details about how this support is provided for .NET source code is discussed below.

Supported Client Libraries

Supported Operations

Operation

Methods Supported

Insert

MongoDB.Driver.IMongoCollection.InsertOne

MongoDB.Driver.IMongoCollection.InsertOneAsync

MongoDB.Driver.IMongoCollection.InsertManyAsync

MongoDB.Driver.IMongoCollection.InsertMany

Update

MongoDB.Driver.IMongoCollection.UpdateManyAsync

MongoDB.Driver.IMongoCollection.UpdateOneAsync

MongoDB.Driver.IMongoCollection.UpdateOne

MongoDB.Driver.IMongoCollection.UpdateMany

MongoDB.Driver.IMongoCollection.ReplaceOne

MongoDB.Driver.IMongoCollection.ReplaceOneAsync

MongoDB.Driver.IMongoCollectionExtensions.ReplaceOne

MongoDB.Driver.IMongoCollectionExtensions.ReplaceOneAsync

MongoDB.Driver.IMongoCollectionExtensions.UpdateMany

MongoDB.Driver.IMongoCollectionExtensions.UpdateManyAsync

MongoDB.Driver.IMongoCollectionExtensions.UpdateOne

MongoDB.Driver.IMongoCollectionExtensions.UpdateOneAsync

Select

MongoDB.Driver.IMongoCollection.Find

MongoDB.Driver.IMongoCollection.FindSync

MongoDB.Driver.IMongoCollection.FindAsync

MongoDB.Driver.IMongoCollection.FindOneAndDelete

MongoDB.Driver.IMongoCollection.FindOneAndDeleteAync

MongoDB.Driver.IMongoCollection.FindOneAndReplace

MongoDB.Driver.IMongoCollection.FindOneAndReplaceAsync

MongoDB.Driver.IMongoCollection.FindOneAndUpdate

MongoDB.Driver.IMongoCollection.FindOneAndUpdateAsync

MongoDB.Driver.IMongoCollection.Count

MongoDB.Driver.IMongoCollection.CountAsync

MongoDB.Driver.IMongoCollection.Aggregate

MongoDB.Driver.IMongoCollection.AggregateAsync

MongoDB.Driver.IMongoCollection.BulkWrite

MongoDB.Driver.IMongoCollection.BulkWriteAsync

MongoDB.Driver.IMongoCollectionExtensions.ReplaceOne

MongoDB.Driver.IMongoCollectionExtensions.ReplaceOneAsync

MongoDB.Driver.IMongoCollectionExtensions.UpdateMany

MongoDB.Driver.IMongoCollectionExtensions.UpdateManyAsync

MongoDB.Driver.IMongoCollectionExtensions.UpdateOne

MongoDB.Driver.IMongoCollectionExtensions.UpdateOneAsync

MongoDB.Driver.IMongoCollectionExtensions.FindOneAndDelete

MongoDB.Driver.IMongoCollectionExtensions.FindOneAndDeleteAsync

MongoDB.Driver.IMongoCollectionExtensions.FindOneAndReplace

MongoDB.Driver.IMongoCollectionExtensions.FindOneAndReplaceAsync

MongoDB.Driver.IMongoCollectionExtensions.FindOneAndUpdate

MongoDB.Driver.IMongoCollectionExtensions.FindOneAndUpdateAsync

MongoDB.Driver.IMongoCollectionExtensions.Distinct

MongoDB.Driver.IMongoCollectionExtensions.DistinctAsync

MongoDB.Driver.IMongoCollectionExtensions.CountDocuments

MongoDB.Driver.IMongoCollectionExtensions.CountDocumentsAsync

MongoDB.Driver.IMongoCollectionExtensions.Find

MongoDB.Driver.IMongoCollectionExtensions.Aggregate

MongoDB.Driver.IMongoCollectionExtensions.Count

MongoDB.Driver.IMongoCollectionExtensions.CountAsync

Delete 

MongoDB.Driver.IMongoCollection.DeleteMany

MongoDB.Driver.IMongoCollection.DeleteManyAsync

MongoDB.Driver.IMongoCollection.DeleteOne

MongoDB.Driver.IMongoCollection.DeleteOneAsync

MongoDB.Driver.IMongoCollection.FindOneAndDelete

MongoDB.Driver.IMongoCollection.FindOneAndDeleteAsync

MongoDB.Driver.IMongoCollectionExtensions.DeleteOne

MongoDB.Driver.IMongoCollectionExtensions.DeleteOneAsync

MongoDB.Driver.IMongoCollectionExtensions.DeleteMany

MongoDB.Driver.IMongoCollectionExtensions.DeleteManyAsync

Objects

IconDescription

DotNet MongoDB database

DotNet MongoDB collection

DotNet unknown MongoDB database

DotNet unknown MongoDB collection
Link typeSource and destination of link Methods supported
belongsTo

From DotNet MongoDB Collection object to DotNet MongoDB Database object



 useInsertLink

Between the caller .NET Method (C#) object and DotNet  MongoDB Collection object

Between the caller .NET Method (C#) object and DotNet unknown MongoDB Collection object


InsertOne

InsertOneAsync

InsertMany

InsertManyAsync

Insert

useUpdateLink

Between the caller .NET Method (C#) object and DotNet  MongoDB Collection object

Between the caller .NET Method (C#) object and DotNet unknown MongoDB Collection object


UpdateManyAsync

UpdateOneAsync

UpdateOne

UpdateMany

ReplaceOne

ReplaceOneAsync

Update

 useSelectLink

Between the caller .NET Method (C#) object and DotNet  MongoDB Collection object

Between the caller .NET Method (C#) object and DotNet unknown MongoDB Collection object


Find

FindSync

FindAsync

FindOne

FindOneAs

FindOneAndDelete

FindOneAndDeleteAync

FindOneAndReplace

FindOneAndReplaceAsync

FindOneAndUpdate

FinOneAndUpdateAsync

BulkWrite

BulkWriteAsync

useDeleteLink 

Between the caller .NET Method (C#) object and DotNet  MongoDB Collection object

Between the caller .NET Method (C#) object and DotNet unknown MongoDB Collection object


DeleteMany

DeleteManyAsync

DeleteOne

DeleteOneAsync

FindOneAndDelete

FindOneAndDeleteAsync

Amazon DocumentDB Identification

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

DotNet 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

DotNet 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:

Database and Collection Creation

static void InsertOneInMongoDB()
        {
        
            MongoClient client = new MongoClient("mongodb://127.0.0.1:27017");
          
            IMongoDatabase db = client.GetDatabase("TestDb1");
     
            IMongoCollection<Person> persons = db.GetCollection<Person>("Persons");
            IMongoCollection<Dog> dogs = db.GetCollection<Dog>("Dogs");
}

Insert Operation

{
            MongoClient client = new MongoClient("mongodb://127.0.0.1:27017");
            IMongoDatabase db = client.GetDatabase("TestDb1");
            IMongoCollection<BsonDocument> dogs = db.GetCollection<BsonDocument>("Dogs");

            string json = "{Name:'大黄',Age:10,Weight:50}";
            BsonDocument d1 = BsonDocument.Parse(json);//Json
            dogs.InsertOne(d1);

}

Delete Operation

 public void DeleteAllPurchases()
        {
            MyShoppingCollection.DeleteMany(FilterDefinition<PurchaseBson>.Empty, null);
        }

Select Operation

public PurchaseBson[] FindPurchases()
        {
            var purchases = MyShoppingCollection.Find(FilterDefinition<PurchaseBson>.Empty, null).ToList();
            return purchases.ToArray();
        }

Update Operation

 public async Task<IActionResult> Update()
        {
            var query = from k in (from a in _context.Books.AsQueryable()
                                    where a.Title == "Test Book 2"
                                    from b in a.Posts
                                    select b)
                         where k.Title == "Second One"
                         select k;
            var result = await query.FirstOrDefaultAsync();
            result.ReadCount = 5;

            _context.Books
                .FindOneAndUpdate(x => x.Title == "Test Book 2" && x.Posts.Any(p => p.Title == "Second One"),
                                    Builders<Book>.Update.Set(x => x.Posts[-1], result));

            return Redirect("/");
        }

Amazon DocumentDB Identification

class Program
    {
       static void Main(string[] args)
        {
           string template = "mongodb://{0}:{1}@{2}/?replicaSet=rs0&readpreference={3}";
           string username = "root";
           string password = "rootroot";
           string clusterEndpoint = "sample-cluster.cluster-cqjaxx9hbhi0.us-west-2.docdb.amazonaws.com:27017";
           string readPreference = "secondaryPreferred";

           string connectionString = String.Format(template, username, password, clusterEndpoint, readPreference);
           var settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
           var client = new MongoClient(settings);

           var database = client.GetDatabase("library");
           var collection = database.GetCollection<BsonDocument>("Books");

           var docToInsert = new BsonDocument { { "title", "Harry Potter" } };
           collection.InsertOne(docToInsert);
        }
    }

Reading Database and Collection from Json File

.cs code

.cs code
 public class CustomerService
    {
        private readonly IMongoCollection<Customer> _customer;
        private readonly DeveloperDatabaseConfiguration _settings;

        public CustomerService(IOptions<DeveloperDatabaseConfiguration> settings)
        {
            _settings = settings.Value;
            var client = new MongoClient(_settings.ConnectionString);
            var database = client.GetDatabase(_settings.DatabaseName);
            _customer = database.GetCollection<Customer>(_settings.CustomerCollectionName);
        }

        public async Task<List<Customer>> GetAllAsync()
        {
            return await _customer.Find(c => true).ToListAsync();
        }
    }

.cs file

.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WorkingWithMongoDB.WebAPI.Configuration
{
    public class DeveloperDatabaseConfiguration
    {
        public string CustomerCollectionName { get; set; }
        public string ConnectionString { get; set; }
        public string DatabaseName { get; set; }
    }
}

json file

Json file
{
  "DeveloperDatabaseConfiguration": {
    "CustomerCollectionName": "Customers",
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "DevelopmentDatabase"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Known Limitations

  • Database and Collections are resolved in following situation: 
    • Anywhere in the .cs files  and JSON files
  • If Database and Collection values are not resolved, will create Unknown Database and Collection objects
  • Setting of properties related to Amazon DocumentDB identification depends on proper resolution of connection string.