本文整理汇总了C#中BsonDocument.TryGetValue方法的典型用法代码示例。如果您正苦于以下问题:C# BsonDocument.TryGetValue方法的具体用法?C# BsonDocument.TryGetValue怎么用?C# BsonDocument.TryGetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonDocument
的用法示例。
在下文中一共展示了BsonDocument.TryGetValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AssertValid
private void AssertValid(ConnectionString connectionString, BsonDocument definition)
{
if (!definition["valid"].ToBoolean())
{
Assert.Fail($"The connection string '{definition["uri"]}' should be invalid.");
}
BsonValue readConcernValue;
if (definition.TryGetValue("readConcern", out readConcernValue))
{
var readConcern = ReadConcern.FromBsonDocument((BsonDocument)readConcernValue);
connectionString.ReadConcernLevel.Should().Be(readConcern.Level);
}
BsonValue writeConcernValue;
if (definition.TryGetValue("writeConcern", out writeConcernValue))
{
var writeConcern = WriteConcern.FromBsonDocument(MassageWriteConcernDocument((BsonDocument)writeConcernValue));
connectionString.W.Should().Be(writeConcern.W);
connectionString.WTimeout.Should().Be(writeConcern.WTimeout);
connectionString.Journal.Should().Be(writeConcern.Journal);
connectionString.FSync.Should().Be(writeConcern.FSync);
}
}
示例2: UpdateDocument
public override void UpdateDocument(MongoCollection<BsonDocument> collection, BsonDocument document) {
var errorCollection = Database.GetCollection(ErrorRepository.CollectionName);
ObjectId stackId = document.GetValue(ErrorStackRepository.FieldNames.Id).AsObjectId;
if (stackId == ObjectId.Empty)
return;
BsonValue value;
bool isHidden = false;
if (document.TryGetValue(ErrorStackRepository.FieldNames.IsHidden, out value))
isHidden = value.AsBoolean;
DateTime? dateFixed = null;
if (document.TryGetValue(ErrorStackRepository.FieldNames.DateFixed, out value))
dateFixed = value.ToNullableUniversalTime();
IMongoQuery query = Query.EQ(ErrorRepository.FieldNames.ErrorStackId, new BsonObjectId(stackId));
var update = new UpdateBuilder();
if (isHidden)
update.Set(ErrorRepository.FieldNames.IsHidden, true);
if (dateFixed.HasValue)
update.Set(ErrorRepository.FieldNames.IsFixed, true);
if (isHidden || dateFixed.HasValue)
errorCollection.Update(query, update);
}
示例3: Map
/// <summary>
/// Maps the specified response to a custom exception (if possible).
/// </summary>
/// <param name="response">The response.</param>
/// <returns>The custom exception (or null if the response could not be mapped to a custom exception).</returns>
public static Exception Map(BsonDocument response)
{
BsonValue code;
if (response.TryGetValue("code", out code) && code.IsNumeric)
{
switch (code.ToInt32())
{
case 50:
case 13475:
case 16986:
case 16712:
return new ExecutionTimeoutException("Operation exceeded time limit.");
}
}
// the server sometimes sends a response that is missing the "code" field but does have an "errmsg" field
BsonValue errmsg;
if (response.TryGetValue("errmsg", out errmsg) && errmsg.IsString)
{
if (errmsg.AsString.Contains("exceeded time limit") ||
errmsg.AsString.Contains("execution terminated"))
{
return new ExecutionTimeoutException("Operation exceeded time limit.");
}
}
return null;
}
示例4: UpdateDocument
public override void UpdateDocument(MongoCollection<BsonDocument> collection, BsonDocument document)
{
var errorCollection = Database.GetCollection("error");
ObjectId stackId = document.GetValue("_id").AsObjectId;
if (stackId == ObjectId.Empty)
return;
BsonValue value;
bool isHidden = false;
if (document.TryGetValue("hid", out value))
isHidden = value.AsBoolean;
DateTime? dateFixed = null;
if (document.TryGetValue("fdt", out value))
dateFixed = value.ToNullableUniversalTime();
IMongoQuery query = Query.EQ("sid", new BsonObjectId(stackId));
var update = new UpdateBuilder();
if (isHidden)
update.Set("hid", true);
if (dateFixed.HasValue)
update.Set("fix", true);
if (isHidden || dateFixed.HasValue)
errorCollection.Update(query, update, UpdateFlags.Multi);
}
示例5: RunTestDefinition
public void RunTestDefinition(BsonDocument definition)
{
BsonValue readConcernValue;
if (definition.TryGetValue("readConcern", out readConcernValue))
{
ValidateReadConcern(definition);
}
BsonValue writeConcernValue;
if (definition.TryGetValue("writeConcern", out writeConcernValue))
{
ValidateWriteConcern(definition);
}
}
示例6: Ins
private static string Ins(BsonDocument doc)
{
try
{
BsonValue id;
if (!doc.TryGetValue("_id", out id))
throw new Exception("fant ikke _id");
var exp = new ExpenseModel
{
IsCommon = doc.GetValue("IsCommon", true).ToBoolean(),
IsPossibleDuplicate = doc.GetValue("IsPossibleDuplicate", false).ToBoolean(),
Means = doc.GetValue("Means", string.Empty).ToString(),
Owner = doc.GetValue("Owner", string.Empty).ToString(),
Amount = doc.GetValue("Amount", 0).ToDouble(),
Description = doc.GetValue("Description", string.Empty).ToString(),
Date = doc.GetValue("Date").AsDateTime
};
var nu = ModelCollection<ExpenseModel>.InsertItem(exp);
return "insert " + nu.Id;
}
catch (Exception ex)
{
return ex.Message;
}
}
示例7: DefaultImplementation
// public static methods
/// <summary>
/// Default implementation of the CanCommandBeSentToSecondary delegate.
/// </summary>
/// <param name="document">The command.</param>
/// <returns>True if the command can be sent to a secondary member of a replica set.</returns>
public static bool DefaultImplementation(BsonDocument document)
{
if (document.ElementCount == 0)
{
return false;
}
var commandName = document.GetElement(0).Name;
if (__secondaryOkCommands.Contains(commandName))
{
return true;
}
if (commandName.Equals("mapreduce", StringComparison.InvariantCultureIgnoreCase))
{
BsonValue outValue;
if (document.TryGetValue("out", out outValue) && outValue.IsBsonDocument)
{
return outValue.AsBsonDocument.Contains("inline");
}
}
return false;
}
示例8: Create
// static methods
public static BulkWriteBatchResult Create(
bool isOrdered,
IReadOnlyList<WriteRequest> requests,
BsonDocument writeCommandResponse,
IndexMap indexMap)
{
var writeErrors = CreateWriteErrors(writeCommandResponse);
var writeConcernError = CreateWriteConcernError(writeCommandResponse);
var processedRequests = CreateProcessedRequests(requests, writeErrors, isOrdered);
var unprocessedRequests = CreateUnprocessedRequests(requests, writeErrors, isOrdered);
var upserts = CreateUpserts(writeCommandResponse);
var n = writeCommandResponse.GetValue("n", 0).ToInt64();
var matchedCount = 0L;
var deletedCount = 0L;
var insertedCount = 0L;
long? modifiedCount = 0L;
var firstRequest = requests.FirstOrDefault();
if (firstRequest != null)
{
switch (firstRequest.RequestType)
{
case WriteRequestType.Delete:
deletedCount = n;
break;
case WriteRequestType.Insert:
insertedCount = n;
break;
case WriteRequestType.Update:
matchedCount = n - upserts.Count();
BsonValue nModified;
if (writeCommandResponse.TryGetValue("nModified", out nModified))
{
modifiedCount = nModified.ToInt64();
}
else
{
modifiedCount = null;
}
break;
}
}
return new BulkWriteBatchResult(
requests.Count,
processedRequests,
unprocessedRequests,
matchedCount,
deletedCount,
insertedCount,
modifiedCount,
upserts,
writeErrors,
writeConcernError,
indexMap);
}
示例9: MapValue
protected int? MapValue(BsonDocument document, string field, int? dontCare)
{
int? intValue = default(int?);
BsonValue bsonField;
if (document.TryGetValue(field, out bsonField))
{
intValue = bsonField.AsNullableInt32;
}
return intValue;
}
示例10: CreateJsonFile
private static void CreateJsonFile(string jsonReportsDirectory, BsonDocument report)
{
report.Remove("_id");
BsonValue productIdAsBson;
report.TryGetValue("product-id", out productIdAsBson);
using (var file = new StreamWriter(jsonReportsDirectory + productIdAsBson.AsInt32 + ".json"))
{
file.WriteLine(report.ToJson());
}
}
示例11: GetString
private static String GetString(BsonDocument doc, String key, String defaultValue = "")
{
BsonValue value;
var contains = doc.TryGetValue(key, out value);
if (!contains || !value.IsString)
return defaultValue;
return value.AsString;
}
示例12: ThrowIfHasWriteConcernError
public static void ThrowIfHasWriteConcernError(ConnectionId connectionId, BsonDocument result)
{
BsonValue value;
if (result.TryGetValue("writeConcernError", out value))
{
var message = (string)value.AsBsonDocument.GetValue("errmsg", null);
var writeConcernResult = new WriteConcernResult(result);
throw new MongoWriteConcernException(connectionId, message, writeConcernResult);
}
}
示例13: KeyOf
public BsonValue KeyOf(BsonDocument document)
{
BsonValue value = null;
if (document.TryGetValue(Field.RESOURCEID, out value))
{
return value;
}
else
{
return null;
}
}
示例14: LoadDataFromBSON
/// <summary>
/// Inits record feld values from BSON document
/// </summary>
public static void LoadDataFromBSON(this Record rec, BsonDocument doc)
{
foreach(var fld in rec.Fields.Where(f => f.StoreFlag == StoreFlag.LoadAndStore || f.StoreFlag == StoreFlag.OnlyLoad))
{
BsonValue val;
if (!doc.TryGetValue(fld.FieldName, out val)) continue;
if (val is BsonDateTime)
fld.ValueAsDateTime = val.ToUniversalTime();// .AsDateTime;
else
fld.ValueAsObject = val.AsBsonValue;//.RawValue;
}
}
示例15: MassageReply
public virtual BsonDocument MassageReply(BsonDocument reply)
{
var massagedReply = new BsonDocument(reply);
BsonValue ok;
if (massagedReply.TryGetValue("ok", out ok))
{
// have to force every ok value into a double since the server
// hasn't chosen a consistent representation.
massagedReply["ok"] = ok.ToDouble();
}
return massagedReply;
}