private static async Task <bool> IndexExists(IMongoContext _mongoContext, string indexName) { return(await _mongoContext.ExecuteAsync <HousePrice, bool>("Transactions", async (collection) => { var collectionManager = collection.Indexes; var indexes = await collectionManager.ListAsync(); while (indexes.MoveNext()) { var currentIndex = indexes.Current; foreach (var doc in currentIndex) { BsonValue val; bool ok = doc.TryGetValue("name", out val); if (ok) { return true; } } } return false; })); }
public async Task <PagedResult <HousePrice> > GetLookups(string postcode, double radius) { Log.Information("Starting retrieval postcode retrieval..."); var postcodeInfo = LogAccessTime(_postcodeLookup.GetByPostcode, postcode, "Postcode lookup of lat and long took {0} milliseconds"); var timer = Stopwatch.StartNew(); if (postcodeInfo?.Longitude != null && postcodeInfo.Latitude != null) { try { Log.Information("Sending request to Mongo..."); var list = await _mongoContext.ExecuteAsync <HousePrice, PagedResult <HousePrice> >("Transactions", async (activeCollection) => { var locationQuery = new FilterDefinitionBuilder <HousePrice>().GeoWithinCenterSphere( tag => tag.Location, postcodeInfo.Longitude.Value, postcodeInfo.Latitude.Value, (radius / 1000) / 6371); var sort = new SortDefinitionBuilder <HousePrice>().Descending(x => x.TransferDate); var query = activeCollection.Find(locationQuery); var result = new PagedResult <HousePrice>(100, await query.Sort(sort).Skip(0).Limit(25).ToListAsync()); Log.Information("Request to mongo successful"); return(result); }); timer.Stop(); Log.Information($"Time to get transaction records was {TimeSpan.FromMilliseconds(timer.ElapsedMilliseconds).ToString()}"); return(list); } catch (Exception ex) { Log.Information($"Error occured accessing Mongodb: {ex.Message}"); } } return(new PagedResult <HousePrice>(0, new HousePrice[0])); }