当前位置: 首页>>代码示例>>C#>>正文


C# IMongoCollection.UpdateOneAsync方法代码示例

本文整理汇总了C#中IMongoCollection.UpdateOneAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IMongoCollection.UpdateOneAsync方法的具体用法?C# IMongoCollection.UpdateOneAsync怎么用?C# IMongoCollection.UpdateOneAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IMongoCollection的用法示例。


在下文中一共展示了IMongoCollection.UpdateOneAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FillData

        private static async Task FillData(
            dynamic profile,
            SqlConnection connection,
            IMongoCollection<BsonDocument> threadCollection,
            IMongoCollection<BsonDocument> threadProfiles)
        {
            var tags = connection.Query<string>(SqlQueryFactory.Instance.Get("get_thread_tags"), new { ThreadId = profile.Id });

            var key = Builders<BsonDocument>.Filter.Eq("id", (profile.Id as string).Trim());

            var match = await threadCollection.Find(key).Project("{url: 1, messages: 1, _id: 0}").SingleOrDefaultAsync();

            var updateAction = Builders<BsonDocument>.Update
                                 .Set("create_on", (DateTime)profile.CreateOn)
                                 .Set("category", profile.Category as string)
                                 .Set("title", profile.Title as string)
                                 .Set("type", profile.Type as string);

            if (match != null)
            {
                var html = match.GetValue("messages")
                                .AsBsonArray
                                .FirstOrDefault()
                                .AsBsonDocument
                                .GetValue("body").AsString;

                var document = new HtmlAgilityPack.HtmlDocument();

                document.LoadHtml(html);

                var text = document.DocumentNode.InnerText;

                var excerpt = text.Substring(0, Math.Min(256, text.Length));

                updateAction = updateAction.Set("url", match.GetValue("url").AsString)
                                           .Set("excerpt", excerpt);
            }

            if (tags != null)
            {
                var tagArray = new BsonArray(tags.Select(m => m.ToLower()).ToList());

                updateAction = updateAction.Set("tags", tagArray);
            }

            await threadProfiles.UpdateOneAsync("{'_id': '" + (profile.Id as string).Trim() + "'}",
                updateAction,
                new UpdateOptions { IsUpsert = true });
        }
开发者ID:Edward-Zhou,项目名称:easy-analysis,代码行数:49,代码来源:BuildThreadProfiles.cs

示例2: DeleteAsync

        public static async Task<int> DeleteAsync(IMongoCollection<BsonDocument> collection, params object[] keyValues)
        {
            if (keyValues.Length == 0)
                throw new ArgumentException("At least 1 keyValue required.");

            if (keyValues.Length == 1)
            {
                var ret = await collection.DeleteOneAsync(Builders<BsonDocument>.Filter.Eq("_id", keyValues[0]));
                return ret != null ? (int)ret.DeletedCount : 0;
            }
            else
            {
                var keyPath = ToDotPath(keyValues.Skip(1));
                var ret = await collection.UpdateOneAsync(Builders<BsonDocument>.Filter.Eq("_id", keyValues[0]),
                                                          Builders<BsonDocument>.Update.Unset(keyPath));
                return ret != null ? (int)ret.ModifiedCount : 0;
            }
        }
开发者ID:cupsster,项目名称:TrackableData,代码行数:18,代码来源:DocumentHelper.cs

示例3: ExecuteAsync

        public async Task ExecuteAsync(UpsertClimbCommandParameters parameters, IMongoCollection<Climb> collection)
        {
            var climb = new Climb
            {
                Id = parameters.Id,
                LatLong = parameters.LatLong,
                Rating = parameters.Rating,
                Styles = parameters.Styles,
                Name = parameters.Name
            };

            if (parameters.Id == Guid.Empty)
            {
                await collection.InsertOneAsync(climb);
            }
            else
            {
                var filter = Builders<Climb>.Filter.Eq(c => c.Id == climb.Id, true);
                await collection.UpdateOneAsync(filter, new ObjectUpdateDefinition<Climb>(climb));
            }
        }
开发者ID:MikeBeastall,项目名称:MonkeyStrong,代码行数:21,代码来源:UpsertClimbCommand.cs

示例4: DoWork

        private async Task DoWork(IMongoCollection<BsonDocument> collection)
        {
            var rand = new Random();
            while (!_cancellationTokenSource.IsCancellationRequested)
            {
                var i = rand.Next(0, 10000);
                List<BsonDocument> docs;
                try
                {
                    docs = await collection.Find(new BsonDocument("i", i))
                        .ToListAsync(_cancellationTokenSource.Token);
                }
                catch
                {
                    Console.Write("+");
                    continue;
                }

                if (docs.Count == 0)
                {
                    try
                    {
                        await collection.InsertOneAsync(new BsonDocument("i", i), _cancellationTokenSource.Token);
                    }
                    catch
                    {
                        Console.Write("*");
                    }
                }
                else
                {
                    try
                    {
                        var filter = new QueryDocument("_id", docs[0]["_id"]);
                        var update = new UpdateDocument("$set", new BsonDocument("i", i + 1));
                        await collection.UpdateOneAsync(filter, update, cancellationToken: _cancellationTokenSource.Token);
                        //Console.Write(".");
                    }
                    catch (Exception)
                    {
                        Console.Write("*");
                    }
                }
            }
        }
开发者ID:kay-kim,项目名称:mongo-csharp-driver,代码行数:45,代码来源:Api.cs


注:本文中的IMongoCollection.UpdateOneAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。