1. Download driver:
For example: Select C # Solution, right click on "Manage NuGet package (N) ..."
In the popup dialog box, enter Mongodb.driver, search, and then select Install.
2. Reference namespace:
Using Mongodb.bson;
Using Mongodb.driver;
Using MongoDB.Driver.Core;
3. Program code
Configuration file:
public class Ictrmongodbcache:cachebase
{
Imongocollection<bsondocument> _collect;
Imongodatabase _db;
String _collectionname = "";
<summary>
Default Search column Field name
</summary>
String defaultcolumn = configurationmanager.appsettings["Defaultcolumn"]. ToString ();
<summary>
constructor function
</summary>
<param name= "CollectionName" > collection name, similar to table name in relational database </param>
Public Ictrmongodbcache (String collectionname)
: Base (CollectionName)
{
_collectionname = CollectionName;
String mongodb_host = configurationmanager.appsettings["Mongodb_host"]. ToString ();
String mongodb_dbname = configurationmanager.appsettings["Mongodb_dbname"]. ToString ();
String mongodb_username = configurationmanager.appsettings["Mongodb_username"]. ToString ();
String Mongodb_password = configurationmanager.appsettings["Mongodb_password"]. ToString ();
MongoDB.Driver.MongoClient mc = new Mongoclient (mongodb_host);
_db = MC. Getdatabase (Mongodb_dbname);
_collect = _db. Getcollection<bsondocument> (CollectionName);
}
public override bool Append (string keyValue, String value)
{
if (string. IsNullOrEmpty (value) | | Value. IndexOf (":") < 0)
{
return false;
}
dictionary<string, string> dictr = new dictionary<string, string> ();
Dictr.add (Defaultcolumn, KeyValue);
Try
{
String[] values = value. Split (', ');
foreach (string keyv in values)
{
Dictr.add (Keyv. Split (': ') [0], Keyv. Split (': ') [1]);
}
bsondocument bd = new bsondocument ();
Bd. AddRange (DICTR);
_collect. Insertone (BD);
}
catch (System.InvalidOperationException ex)
{
Throw ex;
}
return true;
}
public override Object Get (String keyValue)
{
Dictionary<string,object> dict =new dictionary<string,object> ();
Dict. ADD (Defaultcolumn, KeyValue);
var query = new Bsondocument (dict);
iasynccursor<bsondocument> result = _collect. Findsync (query);
list<bsondocument> list = null;
if (result. MoveNext ())
{
List = result. Current as list<bsondocument>;
}
return list;
}
public override T get<t> (string keyValue)
{
dictionary<string, object> dict = new dictionary<string, object> ();
Dict. ADD (Defaultcolumn, KeyValue);
var query = new Bsondocument (dict);
iasynccursor<bsondocument> result = _collect. Findsync (query);
if (result. MoveNext ())
{
list<bsondocument> list = result. Current as list<bsondocument>;
if (list! = null)
{
bsondocument BD = list[0];
Bd. Remove ("_id");
Bd. Remove (Defaultcolumn);
JavaScriptSerializer serializer = new JavaScriptSerializer ();
serializer.maxjsonlength = Int. MaxValue;
Return Serializer.deserialize<t> (Bd. ToString ());
}
Else
{
return default (T);
}
}
Else
{
return default (T);
}
}
public override void Set (string KeyValue, object value, DateTime expiration)
{
dictionary<string, object> dictr = new dictionary<string, object> ();
Dictr.add (Defaultcolumn, KeyValue);
Try
{
bsondocument bd = value. Tobsondocument ();
Bd. AddRange (DICTR);
_collect. Insertone (BD);
}
catch (System.InvalidOperationException ex)
{
Throw ex;
}
}
public override void Set<t> (string keyValue, T value)
{
dictionary<string, object> dictr = new dictionary<string, object> ();
Dictr.add (Defaultcolumn, KeyValue);
Try
{
bsondocument bd = value. Tobsondocument<t> ();
Bd. AddRange (DICTR);
_collect. Insertone (BD);
}
catch (System.InvalidOperationException ex)
{
Throw ex;
}
}
public override void Remove (string key)
{
dictionary<string, object> dictr = new dictionary<string, object> ();
Dictr.add (Defaultcolumn, key);
_collect. Deleteone (New Bsondocument (DICTR));
}
public override void Clear ()
{
_db. Dropcollection (_collectionname);
}
public override void Dispose ()
{
_db. Dropcollection (_collectionname);
_collect = null;
}
}
4. Test code
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using Microsoft.VisualStudio.TestTools.UnitTesting;
Using Mongodbcache.caching;
Namespace UnitTest
{
[TestClass]
public class Ictrmongodbcachetest
{
Private Ictrmongodbcache _cache;
Public Ictrmongodbcachetest ()
{
_cache = new Ictrmongodbcache ("Test1");
}
[TestMethod]
public void Testappend ()
{
ID454 the data corresponding to the primary key field, "Age:43,name:marry the data to be inserted,
Call the cached Append method, and the second parameter must be a data similar to "Column1:column1value,column2:column2value"
_cache. Append ("ID1", "Age:43,name:marry");
}
[TestMethod]
public void TestSet1 ()
{
dictionary<string, string> dict = new dictionary<string, string> ();
Dict. ADD ("Age", "12");
Dict. ADD ("Name", "Marry");
_cache. Set ("ID2", dict);
}
[TestMethod]
public void TestSet2 ()
{
Student s = new student ();
S.age = 234;
S.name = "Lengli";
_cache. Set<student> ("ID3", s);
}
[TestMethod]
public void Testremove ()
{
String ID = "ID1";
_cache. Remove (ID);
}
[TestMethod]
public void TestGet1 ()
{
Object obj = _cache. Get ("ID2");
}
[TestMethod]
public void TestGet2 ()
{
Student DD = _cache. Get<student> ("ID3");
}
[TestMethod]
public void Testclear ()
{
_cache. Clear ();
}
[TestClass]
Class student
{
public string Name
{
Get
Set
}
public int Age
{
Get
Set
}
}
}
}
C # Operations MongoDB database