本文整理汇总了C#中BsonDocument.TryGetElement方法的典型用法代码示例。如果您正苦于以下问题:C# BsonDocument.TryGetElement方法的具体用法?C# BsonDocument.TryGetElement怎么用?C# BsonDocument.TryGetElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonDocument
的用法示例。
在下文中一共展示了BsonDocument.TryGetElement方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetNestedBson
private static BsonElement GetNestedBson(BsonDocument doc, string name)
{
BsonElement element = null;
var levels = name.Split('.');
if (levels.Length > 1)
{
for (int i = 0; i < levels.Length - 1; i++)
{
name = levels[levels.Length - 1];
doc.TryGetElement(levels[i], out element);
if (element != null && element.Value.IsBsonDocument)
doc = element.Value.AsBsonDocument;
}
}
doc.TryGetElement(name, out element);
return element;
}
示例2: UpdateDocument
public override void UpdateDocument(MongoCollection<BsonDocument> collection, BsonDocument document)
{
BsonElement userRoleElement;
if (!document.TryGetElement("UserRole", out userRoleElement))
{
document.Add("UserRole", 0);
}
document.Add("ProjectTokens", new BsonArray());
collection.Save(document);
}
示例3: CompareDocuments
private static bool CompareDocuments(BsonDocument a, BsonDocument b)
{
if (a.ElementCount != b.ElementCount)
{
return false;
}
foreach (var aElement in a)
{
BsonElement bElement;
if (!b.TryGetElement(aElement.Name, out bElement))
{
return false;
}
if (!Compare(aElement.Value, bElement.Value))
{
return false;
}
}
return true;
}
示例4: TestTryGetElement
public void TestTryGetElement()
{
var document = new BsonDocument();
BsonElement element;
Assert.IsFalse(document.TryGetElement("x", out element));
document["x"] = 1;
Assert.IsTrue(document.TryGetElement("x", out element));
Assert.AreEqual("x", element.Name);
Assert.AreEqual(1, element.Value.AsInt32);
}
示例5: HandleDistinctData
private void HandleDistinctData(BsonDocument data, string databaseName, string collectionName)
{
try
{
var databaseInfo = MongodbServerMaintainceCenter.GetDatabaseInfo(databaseName);
if (databaseInfo != null)
{
var collectionInfo = databaseInfo.Collections.SingleOrDefault(c => c.CollectionName == collectionName);
if (collectionInfo != null)
{
lock (collectionInfo.CascadeFilterColumns)
{
foreach (var item in collectionInfo.CascadeFilterColumns)
{
BsonElement element;
data.TryGetElement(item.ColumnName, out element);
if (element != null)
{
var value = element.Value.RawValue.ToString();
if (item.DistinctValues.Count(v => string.Equals(v, value, StringComparison.InvariantCultureIgnoreCase)) == 0)
{
item.DistinctValues.Add(value);
}
}
}
}
lock (collectionInfo.ListFilterColumns)
{
foreach (var item in collectionInfo.ListFilterColumns)
{
var levels = item.ColumnName.Split('.');
BsonDocument doc = data;
BsonElement element;
string name = "";
if (levels.Length > 1)
{
name = levels[levels.Length - 1];
for (int i = 0; i < levels.Length - 1; i++)
{
doc.TryGetElement(levels[i], out element);
if (element != null && element.Value != null)
doc = element.Value.AsBsonDocument;
}
}
else
{
name = item.ColumnName;
}
if (doc != null)
{
doc.TryGetElement(name, out element);
if (element != null)
{
var value = element.Value.RawValue.ToString();
var enumColumnDescription = MongodbServerMaintainceCenter.GetMongodbEnumColumnDescription(databaseInfo.DatabasePrefix, item.ColumnName);
if (enumColumnDescription != null)
{
if (item.DistinctValues.Count(v => Convert.ToInt16(v.Value) == Convert.ToInt16(value)) == 0)
{
item.DistinctValues.Add(new ItemPair
{
Name = enumColumnDescription.EnumItems.FirstOrDefault(enumItem => value == enumItem.Key).Value,
Value = Convert.ToInt16(value),
});
}
}
else
{
if (item.DistinctValues.Count(v => string.Equals(v.Value.ToString(), value, StringComparison.InvariantCultureIgnoreCase)) == 0)
{
item.DistinctValues.Add(new ItemPair
{
Name = value.ToString(),
Value = value.ToString(),
});
}
}
}
}
}
}
}
}
}
catch (Exception ex)
{
LocalLoggingService.Error("{0} {1} {2} {3} {4}", MongodbServerConfiguration.ModuleName, "MongodbServer_Master", "HandleDistinctData", "处理索引数据出现错误", ex.Message);
}
}
示例6: OutputBsonDocument
/// <summary>
/// [MONGODB-DEMO] Create & Output simple BSON Document obj (SQL=row).
/// </summary>
private static void OutputBsonDocument()
{
var doc = new BsonDocument
{
{ "nickname", "JohnnyB"}
};
doc.Add("age", 66);
doc["profession"] = "hacker";
Console.WriteLine(doc);
bool res = doc.Contains("age");
BsonElement element;
if (doc.TryGetElement("nickname", out element))
{
Console.WriteLine(".Name = {0}, .Value = {1} \n\n", element.Name, element.Value);
}
Console.WriteLine();
}