當前位置: 首頁>>代碼示例>>C#>>正文


C# BsonDocument.TryGetValue方法代碼示例

本文整理匯總了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);
            }
        }
開發者ID:narutoswj,項目名稱:mongo-csharp-driver,代碼行數:26,代碼來源:ConnectionStringTestRunner.cs

示例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);
        }
開發者ID:priestd09,項目名稱:Exceptionless,代碼行數:28,代碼來源:v1.0.26.cs

示例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;
        }
開發者ID:Bogdan0x400,項目名稱:mongo-csharp-driver,代碼行數:33,代碼來源:ExceptionMapper.cs

示例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);
        }
開發者ID:arpitgold,項目名稱:Exceptionless,代碼行數:29,代碼來源:v1.0.28.cs

示例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);
            }
        }
開發者ID:narutoswj,項目名稱:mongo-csharp-driver,代碼行數:14,代碼來源:DocumentTestRunner.cs

示例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;
            }
        }
開發者ID:pronning,項目名稱:punch,代碼行數:27,代碼來源:Importer.cs

示例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;
        }
開發者ID:Khosrow-Azizi,項目名稱:MasterExperimentV2,代碼行數:31,代碼來源:CanCommandBeSentToSecondary.cs

示例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);
        }
開發者ID:narutoswj,項目名稱:mongo-csharp-driver,代碼行數:57,代碼來源:BulkWriteBatchResult.cs

示例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;
 }
開發者ID:qanwi1970,項目名稱:dungeon-mart,代碼行數:10,代碼來源:BsonMapper.cs

示例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());
     }
 }
開發者ID:TeamMojito,項目名稱:SupermarketsDatabaseClient,代碼行數:10,代碼來源:MongoDBConnector.cs

示例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;
        }
開發者ID:mmanole,項目名稱:prototype,代碼行數:10,代碼來源:MongoTransitionSerializer.cs

示例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);
     }
 }
開發者ID:mfidemraizer,項目名稱:mongo-csharp-driver,代碼行數:10,代碼來源:WriteConcernErrorHelper.cs

示例13: KeyOf

 public BsonValue KeyOf(BsonDocument document)
 {
     BsonValue value = null;
     if (document.TryGetValue(Field.RESOURCEID, out value))
     {
         return value;
     }
     else
     {
         return null;
     }
 }
開發者ID:Condeti,項目名稱:spark,代碼行數:12,代碼來源:Transaction.cs

示例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;
   }
 }
開發者ID:sergey-msu,項目名稱:nfx,代碼行數:16,代碼來源:MongoDBBSONGenerator.cs

示例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;
        }
開發者ID:RainsSoft,項目名稱:mongo-csharp-driver,代碼行數:13,代碼來源:CrudOperationTestBase.cs


注:本文中的BsonDocument.TryGetValue方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。