本文整理汇总了C#中BsonDocument.SetDocumentId方法的典型用法代码示例。如果您正苦于以下问题:C# BsonDocument.SetDocumentId方法的具体用法?C# BsonDocument.SetDocumentId怎么用?C# BsonDocument.SetDocumentId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonDocument
的用法示例。
在下文中一共展示了BsonDocument.SetDocumentId方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleValue
private static void HandleValue(string key, ComponentConfiguration config, Dictionary<string, object> data)
{
HandleTotalValue(key, config, data);
foreach (var dataItem in data)
{
var itemKey = dataItem.Key;
if (config.ComponentItems.ContainsKey(itemKey))
{
var configItem = config.ComponentItems[itemKey];
var db = server.GetDatabase(key);
var collection = db.GetCollection(itemKey);
var doc = new BsonDocument().Add("V", BsonValue.Create(dataItem.Value));
doc.SetDocumentId(DateTime.Now.ToUniversalTime());
if (configItem.ItemValueType == ItemValueType.TextValue)
{
collection.RemoveAll();
collection.Insert(doc);
}
else if (configItem.ItemValueType == ItemValueType.ExpressionValue)
{
}
else
{
collection.Insert(doc);
}
}
}
}
示例2: TestSetDocumentIdInt32
public void TestSetDocumentIdInt32()
{
var document = new BsonDocument { { "x", "abc" } };
#pragma warning disable 618 // SetDocumentId is obsolete
document.SetDocumentId(1); // in a future release this will be an error because 1 is not a BsonValue
#pragma warning restore
Assert.IsTrue(document["_id"].IsInt32);
Assert.AreEqual(1, document["_id"].AsInt32);
}
示例3: TestSetDocumentIdBsonValue
public void TestSetDocumentIdBsonValue()
{
var document = new BsonDocument { { "x", "abc" } };
var id = BsonInt32.Create(1);
#pragma warning disable 618 // SetDocumentId is obsolete
document.SetDocumentId(id);
#pragma warning restore
Assert.IsTrue(document["_id"].IsInt32);
Assert.AreEqual(1, document["_id"].AsInt32);
}
示例4: Save
private void Save(BsonDocument document)
{
var server = MongoServer.Create("mongodb://192.168.1.114");
var db = server.GetDatabase("mythicalfood");
var collection = db.GetCollection("recipes");
if(!document.IsObjectId)
document.SetDocumentId(Guid.NewGuid());
collection.Save(document);
}
示例5: CreateRole
/// <summary>
/// Adds a new role to the data source for the configured applicationName.
/// </summary>
/// <param name="roleName">The name of the role to create.</param>
public override void CreateRole(string roleName)
{
this.ValidateRoleName(roleName);
var doc = new BsonDocument();
doc.SetDocumentId(roleName.ToLowerInvariant());
var result = this.RoleCollection.Save(doc, SafeMode.True);
if (!result.Ok)
{
throw new ProviderException(String.Format("Could not create role '{0}'. Reason: {1}", roleName, result.LastErrorMessage));
}
}
示例6: StartItem
//.........这里部分代码省略.........
dbb.CreateCollection(aggcollectionname, options);
Thread.Sleep(200);
}
}
catch (Exception ex)
{
ex.Handle("聚合器创建表失败:" + aggcollectionname);
continue;
}
var key = string.Format("{0}.{1}.{2}", dbName, colName, agg.Key);
if (!timers.ContainsKey(key))
{
var timer = new Timer(state =>
{
var a = state as Tuple<string, string, string, TimeSpan>;
if (a != null)
{
try
{
var db = server.GetDatabase(a.Item1);
var collection = db.GetCollection(a.Item2);
var aggcollection = db.GetCollection(a.Item3);
var aggstart = aggcollection.FindAll().SetLimit(1).SetSortOrder(SortBy.Descending("$natural")).FirstOrDefault();
DateTime? startTime = null;
if (aggstart != null)
{
startTime = aggstart["_id"].AsDateTime.ToLocalTime();
}
else
{
var start = collection.FindAll().SetLimit(1).SetSortOrder(SortBy.Ascending("$natural")).FirstOrDefault();
if (start != null)
{
startTime = start["_id"].AsDateTime.ToLocalTime();
}
}
if (startTime != null)
{
var endTime = startTime.Value.Add(a.Item4);
while (endTime < DateTime.Now)
{
var query = Query.LT("_id", endTime).GTE(startTime);
var data = collection.Find(query).ToList();
object v = 0;
if (data.Count > 0)
{
Func<BsonDocument, long> func = b =>
{
if (b["V"].IsInt64)
return b["V"].AsInt64;
if (b["V"].IsInt32)
return b["V"].AsInt32;
else return 0;
};
switch (type)
{
case "Min":
v = data.Select(func).Min();
break;
case "Max":
v = data.Select(func).Max();
break;
case "Avg":
v = Convert.ToInt32(data.Select(func).Average());
break;
case "Sum":
v = data.Select(func).Sum();
break;
default:
break;
}
}
var doc = new BsonDocument().Add("V", BsonValue.Create(v));
doc.SetDocumentId(endTime.ToUniversalTime());
aggcollection.Insert(doc);
startTime = endTime;
endTime += a.Item4;
}
}
}
catch (Exception ex)
{
ex.Handle(string.Format("{3} PerformanceAggregator出错:{0} {1} {2}", dbName, colName, agg.Key, config.Name));
}
}
}, new Tuple<string, string, string, TimeSpan>(dbName, colName, aggcollectionname, agg.Value), TimeSpan.Zero, agg.Value);
timers.Add(key, timer);
LocalLoggingService.Info(string.Format("{3} PerformanceAggregator成功初始化:{0} {1} {2}", dbName, colName, agg.Key, config.Name));
}
}
}
}
}
示例7: HandleData
private void HandleData(Dictionary<string, Dictionary<string, Dictionary<string, List<int>>>> data, string name, string type)
{
foreach (var appKey in new List<string>(data.Keys))
{
var app = data[appKey];
foreach (var itemKey in new List<string>(app.Keys))
{
var item = app[itemKey];
var db = server.GetDatabase(GetDatabaseName(name, appKey, itemKey));
foreach (var subItemKey in new List<string>(item.Keys))
{
var subItem = item[subItemKey];
Int32 value = 0;
if (subItem != null && subItem.Count > 0)
{
lock (subItem)
{
switch (type)
{
case "Min":
value = subItem.Min();
break;
case "Max":
value = subItem.Max();
break;
case "Avg":
value = Convert.ToInt32(subItem.Average());
break;
case "Sum":
value = subItem.Sum();
break;
default:
break;
}
}
}
var colName = subItemKey;
var col = db.GetCollection(colName);
var options = CollectionOptions
.SetCapped(true)
.SetAutoIndexId(true)
.SetMaxSize(1024 * 1024 * 100)
.SetMaxDocuments(1000000);
try
{
while (!db.CollectionExists(colName))
{
db.CreateCollection(colName, options);
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
ex.Handle("收集器创建表失败:" + colName);
continue;
}
//AppInfoCenterService.LoggingService.Info(string.Format("收集器成功初始化表:{0} 参数:{1}", col.FullName, options.ToString()));
try
{
var doc = new BsonDocument().Add("V", BsonValue.Create(value));
doc.SetDocumentId(DateTime.Now.ToUniversalTime());
col.Insert(doc);
}
catch (Exception ex)
{
ex.Handle();
}
finally
{
lock (subItem)
subItem.Clear();
}
}
}
}
}
示例8: StartItem
private static void StartItem(KeyValuePair<string, ComponentConfiguration> item)
{
var value = item.Value;
var key = item.Key;
if (value != null && !string.IsNullOrWhiteSpace(value.Url))
{
var db = server.GetDatabase(key);
foreach (var agg in value.AggregateSpans)
{
foreach (var configItem in value.ComponentItems)
{
var aggcollectionname = string.Format("{0}__{1}", configItem.Key, agg.Key);
while (!db.CollectionExists(aggcollectionname))
{
var options = CollectionOptions
.SetCapped(true)
.SetAutoIndexId(false)
.SetMaxSize(1024 * 1024 * 10)
.SetMaxDocuments(100000);
db.CreateCollection(aggcollectionname, options);
Thread.Sleep(200);
}
}
var timer = new Thread(state =>
{
while (true)
{
var t = state as Tuple<KeyValuePair<string, TimeSpan>, ComponentConfiguration>;
if (t != null)
{
var aggInfo = t.Item1;
var config = t.Item2;
try
{
foreach (var configItem in config.ComponentItems)
{
if (configItem.Value.ItemValueType == ItemValueType.TotalValue ||
configItem.Value.ItemValueType == ItemValueType.StateValue)
{
var collection = db.GetCollection(configItem.Key);
var aggcollectionname = string.Format("{0}__{1}", configItem.Key, aggInfo.Key);
var aggcollection = db.GetCollection(aggcollectionname);
var aggstart = aggcollection.FindAll().SetLimit(1).SetSortOrder(SortBy.Descending("$natural")).FirstOrDefault();
DateTime? startTime = null;
if (aggstart != null)
{
startTime = aggstart["_id"].AsDateTime.ToLocalTime();
}
else
{
var start = collection.FindAll().SetLimit(1).SetSortOrder(SortBy.Ascending("$natural")).FirstOrDefault();
if (start != null)
{
startTime = start["_id"].AsDateTime.ToLocalTime();
}
}
if (startTime != null)
{
var endTime = startTime.Value.Add(aggInfo.Value);
while (endTime < DateTime.Now)
{
var query = Query.LT("_id", endTime).GTE(startTime);
var data = collection.Find(query).ToList();
object v = 0;
if (data.Count > 0)
{
Func<BsonDocument, long> func = a =>
{
if (a["V"].IsInt64)
return a["V"].AsInt64;
if (a["V"].IsInt32)
return a["V"].AsInt32;
else return 0;
};
if (configItem.Value.ItemValueType == ItemValueType.TotalValue)
v = data.Select(func).Sum();
if (configItem.Value.ItemValueType == ItemValueType.StateValue)
v = data.Select(func).Average();
}
var doc = new BsonDocument().Add("V", BsonValue.Create(v));
doc.SetDocumentId(endTime.ToUniversalTime());
aggcollection.Insert(doc);
startTime = endTime;
endTime += aggInfo.Value;
}
}
}
}
}
catch (Exception ex)
{
ex.Handle("聚合器" + key);
}
finally
{
Thread.Sleep(aggInfo.Value);
}
}
//.........这里部分代码省略.........
示例9: TestSetDocumentIdNewElement
public void TestSetDocumentIdNewElement()
{
var document = new BsonDocument("x", 1);
document.SetDocumentId(BsonValue.Create(2));
Assert.AreEqual(2, document.ElementCount);
Assert.AreEqual("_id", document.GetElement(0).Name);
Assert.AreEqual(2, document["_id"].AsInt32);
}
示例10: TestSetDocumentId
public void TestSetDocumentId()
{
var document = new BsonDocument("_id", 1);
document.SetDocumentId(BsonValue.Create(2));
Assert.AreEqual(2, document["_id"].AsInt32);
}