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


C# ExpandoObject.ToBsonDocument方法代碼示例

本文整理匯總了C#中System.Dynamic.ExpandoObject.ToBsonDocument方法的典型用法代碼示例。如果您正苦於以下問題:C# ExpandoObject.ToBsonDocument方法的具體用法?C# ExpandoObject.ToBsonDocument怎麽用?C# ExpandoObject.ToBsonDocument使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Dynamic.ExpandoObject的用法示例。


在下文中一共展示了ExpandoObject.ToBsonDocument方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: AddOrReplace

        /// <summary>
        /// Adds or replaces a document within the specified collection.
        /// </summary>
        /// <param name="id">The id of the document to add or replace. If null the document will be added with a newly generated id.</param>
        /// <param name="collection">The name of the collection to add or replace the document within.</param>
        /// <param name="document">The document to add or replace. Note: These must be able to be serialized as a BsonDocument.</param>
        /// <returns>The id of the document if it was added or replaced, null otherwise.</returns>
        public string AddOrReplace(string id, string collection, ExpandoObject document)
        {
            if (!string.IsNullOrWhiteSpace(collection) && isCollectionNameValid(collection) && document != null)
            {
                try
                {
                    MongoCollection mongoCollection = database.GetCollection<BsonDocument>(collection);

                    // Create a BsonDocument to save from the ExpandoObject.
                    BsonDocument bsonDocument = document.ToBsonDocument();

                    // Check that the supplied document contains a ID_FIELD_NAME property.
                    if (!bsonDocument.Names.Contains(ID_FIELD_NAME))
                    {
                        if (string.IsNullOrEmpty(id))
                        {
                            // Add an ID_FIELD_NAME property to the document with a newly generated Id.
                            bsonDocument.Add(ID_FIELD_NAME, ObjectId.GenerateNewId());
                        }
                        else
                        {
                            // Add an ID_FIELD_NAME property to the document with the supplied id.
                            bsonDocument.Add(ID_FIELD_NAME, new ObjectId(id));
                        }
                    }

                    // Add or replace the document.
                    WriteConcernResult result = mongoCollection.Save(bsonDocument);
                    if (result.Ok)
                    {
                        // The document was added or replaced.
                        return ((ObjectId)bsonDocument[ID_FIELD_NAME]).ToString();
                    }
                    else
                    {
                        // There was an error adding or replacing the document.
                        return null;
                    }
                }
                catch (Exception)
                {
                    // There was an error adding or replacing the document in the collection.
                    return null;
                }
            }
            else
            {
                // A null id or collection was supplied or the collection name was invalid.
                return null;
            }
        }
開發者ID:TheHunter,項目名稱:Galactic,代碼行數:58,代碼來源:MongoDBUtility.cs


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