Page tree
Skip to end of metadata
Go to start of metadata

CAST supports MongoDB via its NoSQL for .NET extension. Details about how this support is provided for .NET source code is discussed below.

Supported Client Libraries

Supported Operations

Operation

Methods Supported

Insert
 Insert methods Suppported
  • MongoDB.Driver.IMongoCollection.InsertOne
  • MongoDB.Driver.IMongoCollection.InsertOneAsync
  • MongoDB.Driver.IMongoCollection.InsertManyAsync
  • MongoDB.Driver.IMongoCollection.InsertMany
Update
 Update Methods Supported

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
 Select Methods Supported

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 
 Delete Methods Supported

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

Links are created for transaction and function point needs:

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
 Update Methods Supported

UpdateManyAsync

UpdateOneAsync

UpdateOne

UpdateMany

ReplaceOne

ReplaceOneAsync

Update

 useSelectLink
 Select Methods Supported

Find

FindSync

FindAsync

FindOne

FindOneAs

FindOneAndDelete

FindOneAndDeleteAync

FindOneAndReplace

FindOneAndReplaceAsync

FindOneAndUpdate

FinOneAndUpdateAsync

BulkWrite

BulkWriteAsync

useDeleteLink 
 Delete Method Supported

DeleteMany
DeleteManyAsync
DeleteOne
DeleteOneAsync
FindOneAndDelete
FindOneAndDeleteAsync

What results can you expect?

Once the analysis/snapshot generation has completed, you can view the results in the normal manner (for example via CAST Enlighten). Some examples are shown below.

Database and Collection Creation

 Database and Collection
Database and Collection
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

 Insert operation
Insert
{
            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

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

Select Operation

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

Update Operation

 Update Operation
Update
 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("/");
        }

Reading Database and Collection from Json File

 Reading Database and collection through Json files
.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
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
{
  "DeveloperDatabaseConfiguration": {
    "CustomerCollectionName": "Customers",
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "DevelopmentDatabase"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Violations in CAST Engineering Dashboard

The CAST Engineering Dashboard recognises code violations in source applications.

Evolution

  • Database and collection objects are resolved in maximum cases
  • Support for all crud links for the library Mongodb.driver 
  • Reading database and collection values through JSON

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 
  • No labels