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 }); }
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; } }
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)); } }
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("*"); } } } }