***************************************************************
[BsonIgnoreExtraElements]
public class MyType
{
public BsonObjectId _id { get; set; }
public BsonString text { get; set; }
}
With this approach, the type and user properties are going to be ignored.
**************************************************************************
Add a "catch all" property for extra elements
If you'd rather not ignore those elements, then you could add a catch all property that will house all your extra "undeclared" properties in a bson document.
public class MyType
{
public BsonObjectId _id { get; set; }
public BsonString text { get; set; }
[BsonExtraElements]
public BsonDocument CatchAll { get; set; }
}
With this approach, type and user will exist as properties of the .CatchAll property.
What's the Advantage?
One big advantage is that you can do a "findAndReplace" with the latter without losing the data from fields
you aren't mapping.
****************************** database.RunCommand ****************************************************
var database = mongoServer.GetDatabase("admin");
var res = database.RunCommand("replSetGetStatus");
************************* convert string to BsonDocument *****************************************
var json = "{'_id': 3456}";
var doc= BsonDocument.Parse(json);
var query = new QueryDocument(doc);
var result = coll.Find(query);
************************ convert BsonDocument to string *******************************************
var addr = names[0].address.AsBsonDocument;
string rc = addr.ToJson<MongoDB.Bson.BsonDocument>();
************************* Get data from BsonDocument *****************************************************
var collection = database.GetCollection<BsonDocument>("student");
var names = new BsonDocument();
var query = Query.EQ("_id", "52424201001");
names = collection.FindOne(query);
//for (int i = 0; i < names.Count; i++)
// {
var addr = names["old_school"].AsBsonDocument;
//names[i].results1 = addr.AsString;
//var addr2 = addr.old_school.AsBsonDocument;
Label6.Text = names.ToString();
//}
Label1.Text = names["_id"].ToString() + " " + names["name"].ToString() + " " + addr["namesc"].ToString();
********************************************* datatable ************************************************************
Dim arrrow() As DataRow
strexp = "equip_id='" & ds.Tables("equip_detail").Rows(ppp)(0) & "'"
arrrow = ds.Tables("borrow_detail").Select(strexp)
If arrrow.Length > 0 Then 'if 1
For x As Integer = 0 To arrrow.Length - 1 'for 2
If mdate = arrrow(x)(3).ToString() Then ' if 2
'If mdate = ds.Tables("borrow").Rows(x)(3).ToString() Then ' if 2
Dim tstart = arrrow(x)(4)
Dim tend = arrrow(x)(5)
'Dim tstart As Integer = ds.Tables("borrow").Rows(x)(4)
'Dim tend As Integer = ds.Tables("borrow").Rows(x)(5)
For y As Integer = tstart To tend - 1
objrow1(y - 6) = "~/upload/reserv.jpg"
Next y 'y
'Label3.Text += ds.Tables("borrow").Rows(x)(0).ToString() & "<br />"
End If 'end if 2
Next x 'end for 2
End If 'end if 1
****************************************** Add data from MongoDB to Datatable **************************************
DataTable dt = new DataTable();
dt.Columns.Add("_id", typeof(MongoDB.Bson.ObjectId));
dt.Columns.Add("DA_TrackerID", typeof(Int32));
dt.Columns.Add("DA_Group", typeof(string));
dt.Columns.Add("DA_SubGroup", typeof(string));
dt.Columns.Add("DA_SearchEngine", typeof(string));
dt.Columns.Add("DA_WebSiteURL", typeof(string));
dt.Columns.Add("DA_ArticleHeader", typeof(string));
dt.Columns.Add("DA_ArticleDescription", typeof(string));
dt.Columns.Add("DA_ArticleDetails", typeof(string));
dt.Columns.Add("DA_CompanyName", typeof(string));
dt.Columns.Add("DA_AdditionalKeyword", typeof(string));
dt.Columns.Add("DA_ArticleDateTime", typeof(string));
dt.Columns.Add("DA_ArticleYear", typeof(string));
dt.Columns.Add("DA_ArticleMonth", typeof(string));
dt.Columns.Add("DA_ArticleDay", typeof(string));
dt.Columns.Add("DA_DateTime", typeof(DateTime));
foreach (var item in cursor)
{
dt.Rows.Add(item["_id"], item["DA_TrackerID"], item["DA_Group"], item["DA_SubGroup"], item["DA_SearchEngine"],
item["DA_WebSiteURL"], item["DA_ArticleHeader"], item["DA_ArticleDescription"], item["DA_ArticleDetails"], item["DA_CompanyName"]
, item["DA_AdditionalKeyword"], item["DA_ArticleDateTime"], item["DA_ArticleYear"], item["DA_ArticleMonth"], item["DA_ArticleDay"]
, item["DA_DateTime"]);
}
grid1.DataSource = dt;
************************************* search in list ****************************
int idx = names.FindIndex(x => x._id.StartsWith("322322",StringComparison.InvariantCultureIgnoreCase));
Label2.Text = idx.ToString();
*********************************** options update *********************************
//var options = new UpdateOptions {IsUpsert = true};
******************************************************************************************
// $set $
protected void Button7_Click(object sender, EventArgs e)
{
var client = new MongoClient(Globals.connectionString);
var database = client.GetDatabase("sales");
var collection = database.GetCollection<BsonDocument>("customers");
var _idx = "";
var namex = TextBox5.Text.Trim();
var editx = TextBox6.Text.Trim();
var filter = new BsonDocument { { "_id", _idx }, { "tags", namex } };
var update = Builders<BsonDocument>.Update.Set("tags.$", editx);
try
{
collection.UpdateOne(filter, update);
Label1.Text = " Ҫԡ ó ";
}
catch (Exception ex) { Label1.Text = ex.Message; }
}
*********************************************************************************************
// Pull
protected void Button6_Click(object sender, EventArgs e)
{
var client = new MongoClient(Globals.connectionString);
var database = client.GetDatabase("sales");
var collection = database.GetCollection<BsonDocument>("customers");
var _idx = "";
var namex = TextBox5.Text.Trim();
var filter = new BsonDocument { { "_id", _idx } };
var update = Builders<BsonDocument>.Update.Pull("tags", namex);
try
{
collection.UpdateOne(filter, update);
Label1.Text = "ź Ҫԡ ó ";
}
catch (Exception ex) { Label1.Text = ex.Message; }
}
******************************** ҧ dataset Ѻ reportviwer ********************************************
ReportViewer1.LocalReport.ReportPath = "Report.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();
var client = new MongoClient(Globals.connectionString);
var database = client.GetDatabase("sales");
var collection = database.GetCollection<Class2>("report");
var names = new List<Class2>();
var json2 = "{}";
//var json2 = "{'_id':'123336'}";
var doc2 = BsonDocument.Parse(json2);
var filter = new QueryDocument(doc2);
names = collection.Find(filter).ToList();
ReportDataSource rptds = new ReportDataSource();
rptds.Name = "DataSet2";
rptds.Value = names;
ReportViewer1.LocalReport.DataSources.Add(rptds);
ReportViewer1.LocalReport.Refresh();
******************************************************** auth *****************************************
public static string connectionString = "mongodb://root:passwd@localhost:27017"; // ҧ
******************************************************** create service with auth *********************
mongod.cfg
*******************
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
********************************************************************************************************
sc.exe create MongoDB binPath= "\"C:\MongoDB360\bin\mongod.exe\" --service --auth --config=\"C:\data\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"