当前位置: 首页>>代码示例>>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;未经允许,请勿转载。