Opening a MongoDB GridFS by name with C# Driver - c#

In pymongo, there is an option to open a GridFS with a specific collection name. E.g. mygridfs = gridfs.GridFS(db, collection = mycolc).
I cannot find a similar option in the MongoDB C# driver (official MongoDB latest driver version).
As a result if I want to share GridFS data between Python and C# modules, I can only use the default GridFS in the DB (named 'fs').
Any clues to whether I can somehow access a GridFS with a non-default name in the C# MongoDB driver ?

A sample using grid in c#:
var url = new MongoUrl("mongodb://localhost");
var Client = new MongoClient(url);
var Server = Client.GetServer();
var Database = Server.GetDatabase("test");
var collection = Database.GetCollection("test");
var set = new MongoGridFSSettings {UpdateMD5 = true, ChunkSize = 512*1024, VerifyMD5 = false};
// you can set the name here
set.Root = "mycol";
var grid = Database.GetGridFS(set);
// Upload
grid.Upload(#"C:\Users\Hamid\Desktop\sample.txt", "remote");
// Download
grid.Download(#"C:\Users\Hamid\Desktop\sample2.txt", "remote");

Related

What is the best way to load a model in Tensorflow.NET

I saved a tensorflow.keras model in python and need to use in in C# / Tensorflow.NET 0.15
var net = tf.keras.models.load_model(net_name) does not seem to be implemented
var session = tf.Session.LoadFromSavedModel(net_name);
var graph = sess.graph;
seems to work but I have then a session / graph not a keras model
I would ideally like to call something like net.predict(x), how can I get there from a graph/session ?
Yes, i Did. The best way is to convert you package to the ONNX format. ONNX is a open source format that is supposed to run on any framework (tensorflow, torch...)
In python, add the package onnx and keras2onnx:
import onnx
import keras2onnx
import onnxruntime
net_onnx = keras2onnx.convert_keras(net_keras)
onnx.save_model(net_onnx, onnx_name)
Then in C# .NET, install the nuget Microsoft.ML.
var context = new MLContext();
var session = new InferenceSession(filename);
float[] sample;
int[] dims = new int[] { 1, sample_size};
var tensor = new DenseTensor<float>(sample,dims);
var xs = new List<NamedOnnxValue>()
{
NamedOnnxValue.CreateFromTensor<float>("dense_input", tensor),
};
using (var results = session.Run(xs))
{
// manipulate the results
}
Note that you need to call explicitly the fist layer or the input layer of your network to pass on the sample. best is to give it a nice name in Keras. You can check the name in python by running net_keras.summary()

ZStandard Compression not working in MongoDB v4.2.7

Setup:
I'm using MongoDB v4.2.7 along with the .Net MongoDB driver v2.11.0(beta v) on a windows 10 machine.
Code
var mongoClientSettings = new MongoClientSettings();
mongoClientSettings.Compressors = new List<CompressorConfiguration>() {
new CompressorConfiguration(CompressorType.ZStandard)
};
var client = new MongoClient(mongoClientSettings);
IMongoDatabase testdb = client.GetDatabase("testdb");
var eaiRequestLogsCollection = testdb.GetCollection<EAIRequestsLogMDB>("EAIRequestsLogs");
eaiRequestLogsCollection.InsertMany(eAIRequestsLogMDBs);
Config
I've edited my mongod.cfg file as follows:
storage:
dbPath: C:\Program Files\MongoDB\Server\4.2\data
journal:
enabled: true
engine: "wiredTiger"
wiredTiger:
collectionConfig:
blockCompressor: "zstd"
Problem:
After the collection and the documents are added successfully I ran the db.printCollectionStats() on the mongo shell and I got block_compressor=snappy in the WiredTiger section while it should be block_compressor=zstd.
Below is a screenshot of the db.Stats(1024*1024*1024) output as well
"dataSize" : 0.08773485571146011 and "storageSize" : 0.009387969970703125
I posted a question on MongoDB Developer community Forums and was advised that my c# code was setting Network Compression rather than block_compressor.
This is the correct c# code that worked for me:
var client = new MongoClient("mongodb://127.0.0.1:27017");
IMongoDatabase testdb = client.GetDatabase("testdb");
testdb.CreateCollection("EAIRequestsLogs", new CreateCollectionOptions()
{
StorageEngine = new BsonDocument
{
{ "wiredTiger", new BsonDocument
{
{ "configString" , "block_compressor=zstd"}
}
}
}
});
var eaiRequestLogsCollection = testdb.GetCollection<EAIRequestsLogMDB>("EAIRequestsLogs");
Kind regards.

MongoDb Connect to Replica Set Primary Issue C#

How do I get back the name of the primary database? Lets say database3 was primary
Thanks
var connString = "mongodb://database1,database2,database3/?replicaSet=repl";
var client = new MongoClient(connString);
var server = client.GetServer().Instances.FirstOrDefault(server => server.IsPrimary);
var address = server.Address;
Having looked at the source code of the MongoDB driver, there is no straightforward way to get the name of the primary server from the driver itself. However, you can query server name in MOngoDB by executing {isMaster:1} using RunCommand. You can then parse the primary server from the returned JSON document. This approach works regardless if you are connected to the primary or secondary server.
var mongoClient = new MongoClient(clientSettings);
var testDB = mongoClient.GetDatabase("test");
var cmd = new BsonDocument("isMaster", "1");
var result = testDB.RunCommand<BsonDocument>(cmd);
var primaryServer = result.Where(x => x.Name == "primary").FirstOrDefault().Value.ToString();
Thanks to Jaco for putting me on the right track I solved my problem by doing the following
public static string GetPrimaryDatabase()
{
var mongoClient = new MongoClient(clientSettings);
var server = mongoClient.GetServer();
var database = server.GetDatabase("test");
var cmd = new CommandDocument("isMaster", "1");
var result = database.RunCommand(cmd);
return result.Response.FirstOrDefault(
response => response.ToString().Contains("primary")).Value.ToString();
}
You really should not handle connecting to the right server yourself. The MongoDB driver handles that for you.
Just specify all of your servers in the connections string and the driver will connect to one of them and get replica set's current state on its own. The driver will then direct write operations to the current primary. Read operations may be directed to the primary, or any other server, depending on the read preference you specify.
You can read about forming replica set connection strings here: https://docs.mongodb.org/v3.0/reference/connection-string/

How to copy mongo database with all it's collection using c# driver?

I am trying to copy a mongo database using copydb command using c# driver.
But it's just create the target db with no collection inside.
When i run the command directly with the mongo shell it work fine.
These is the c# code:
var db = mongo.GetServer().GetDatabase("admin");
var command = new CommandDocument(new BsonElement("copydb", 1),
new BsonElement("fromdb", "db1"),
new BsonElement("todb", "db2")
);
var result = db.RunCommand(command);
Its not copy the collections of db1.
These is the command i run into mongo shell and work fine:
db.runCommand({copydb:1, fromdb:"db1", todb:"db2"})
What am i missing?
Try following :
var result = db.RunCommand(
new CommandDocument(new BsonElement("copydb", 1),
new BsonElement("fromhost", "localhost"),
new BsonElement("fromdb", "sourcedb"),
new BsonElement("todb", "targetdb")));
Next code works. It's C# MongoDB.Driver 2.0
var database = mongoClient.GetDatabase("admin");
var command = #"{ copydb: 1, fromhost: 'localhost', fromdb: 'from', todb: 'toDbName'}";
await database.RunCommandAsync<BsonDocument>(BsonDocument.Parse(command));

How to create a WMI filter in a GPO via C#

I'm trying to create a GPO programatically using the GPMC COM object via C# code. I can create the GPO, but I'm having trouble "inserting" a WMI filter with the GPO. Does anyone know how I can create/update WMI filters for GPOs?
Here's the sample code:
GPMGMTLib.GPM gPM = new GPMGMTLib.GPM();
GPMConstants gPMConstants = gPM.GetConstants();
GPMDomain gPMDomain = gPM.GetDomain(domainName, DC, gPMConstants.UseAnyDC);
GPMGPO obj = gPMDomain.CreateGPO();
obj.DisplayName = "New GPO";
//replace with the appropiate GUID
var strWMIFilterID = "{D715559A-7965-45A6-864D-AEBDD9934415}";
var sWMIFilter = string.Format("MSFT_SomFilter.Domain=\"{0}\",ID=\"{1}\"", domainName, strWMIFilterID);
var oWMIFilter = gPMDomain.GetWMIFilter(sWMIFilter);
obj.SetWMIFilter(oWMIFilter);
Here are some links with additional information:
WMIFilters
Active Directory Cookbook

Categories

Resources