var = "This is a string" varName = 'var' s= locals()[varName] s 'This is a string' s2=vars()[varName] s2 'This is a string' s3=eval(varName) s3 'This is a string'
WARNING Other methods to create a .NET binding to a C# library exist. A notorious one is based on the compilation of the C++ library with the C++/CLI compiler. To keep things compatible with the Apple macOS, and because the IJW (it just works) technology sometimes does not, we suggest the use of PInvoke.
Platform Invoke (PInvoke)
allows .NET code to call functions that are implemented in a C/C++ DLL (Windows) or dylib (macOS).
WARNING Other methods to create a .NET binding to a C# library exist. A notorious one is based on the compilation of the C++ library with the C++/CLI compiler. To keep things compatible with the Apple macOS, and because the IJW (it just works) technology sometimes does not, we suggest the use of PInvoke.
vs2019 dependencies - righ click menu NuGet Packages
search ‘MongoDB.Driver’ and install
连接
using MongoDB.Driver;
// mongodb://localhost:27017 mongodb://192.168.0.xx:27017 MongoClient dbClient = new MongoClient("mongodb://localhost");
var dbList = dbClient.ListDatabases().ToList();
var database = dbClient.GetDatabase("stockServiceDoc"); var collection = database.GetCollection<BsonDocument>("capital");
查询
var cursor = collection.Find(highExamScoreFilter).ToCursor(); foreach (var document in cursor.ToEnumerable()) { Console.WriteLine(document); }
// all documents var documents = collection.Find(new BsonDocument()).ToList();
// 正则过滤, 名字 _day 结尾 var dbDatas = client.GetDatabase("stockDataAKShare"); var bre = new BsonRegularExpression("_day$"); var copt = new ListCollectionsOptions { Filter = Builders<BsonDocument>.Filter.Regex("name", bre) }; var collectionsWithMatchingNames = dbDatas.ListCollections(copt).ToList().Select(col => col["name"]); foreach (var coll in collectionsWithMatchingNames) { // find one document. fetch data var doc = database.GetCollection<Model_stock_rank>(coll.ToString()); var results = doc.Find(x => x.date == date); }
排序
var sort = Builders<BsonDocument>.Sort.Descending("student_id");
var highestScores = collection.Find(highExamScoreFilter).Sort(sort); var highestScore = collection.Find(highExamScoreFilter).Sort(sort).First();
Console.WriteLine(highestScore);
插入
还没找到插入失败的处理方式。遇到问题了在处理吧
public int save_capital(ref Capital obj) { var database = dbClient.GetDatabase("stockServiceDoc"); var collection = database.GetCollection<BsonDocument>("capital"); collection.InsertOne(obj.ToBsonDocument()); m_capital.Insert(0,obj); return 0; }