private void SafeDropIndex(IndexKeysBuilder<Person> indexKeysBuilder) { try { mongo.PeopleCollection.DropIndex(indexKeysBuilder); } catch { } }
public void EnsureIndex(Dictionary<string, object> matchData) { // matchKeys, Interval, Left IndexKeysBuilder ikb = new IndexKeysBuilder(); ikb = ikb.Ascending(matchData.Keys.ToArray()); CheckConnection(); Collection.EnsureIndex(ikb); // var indexE = IndexKeys<MongoRange>.Ascending(r => r.SecondsInterval).Ascending(r => r.Left); Collection.EnsureIndex(indexE); }
private void SeedCities() { MongoCollection<BsonDocument> citiesCollection = this.Database.GetCollection("Cities"); IndexKeysBuilder indexKey = new IndexKeysBuilder().Ascending("Name"); citiesCollection.CreateIndex(indexKey, IndexOptions.SetUnique(true)); if (citiesCollection.Count() == 0) { this.CitiesSeeds = new City[] { new City("Pernik"), new City("Qmbol"), new City("Vidin") }; citiesCollection.InsertBatch<City>(this.CitiesSeeds); } }
public static IndexKeysBuilder GetIndex(TaskQueue.TQItemSelector selector) { IndexKeysBuilder ikb = new IndexKeysBuilder(); foreach (KeyValuePair<string, TaskQueue.TQItemSelectorParam> kv in selector.parameters) { switch (kv.Value.ValueSet) { case TaskQueue.TQItemSelectorSet.Equals: Type kvt = kv.Value.Value.GetType(); if (kvt == typeof(bool)) { if ((bool)kv.Value.Value) { ikb = ikb.Ascending(kv.Key); } else { ikb = ikb.Descending(kv.Key); } } else { ikb = ikb.Ascending(kv.Key); } break; } } foreach (KeyValuePair<string, TaskQueue.TQItemSelectorParam> kv in selector.parameters) { switch (kv.Value.ValueSet) { case TaskQueue.TQItemSelectorSet.Ascending: ikb = ikb.Ascending(kv.Key); break; case TaskQueue.TQItemSelectorSet.Descending: ikb = ikb.Descending(kv.Key); break; } } return ikb; }
/// <summary> /// Initializes all the needed indexes for the pages to optimize search on them. /// </summary> public void InitializeIndexes() { // key options var options = new IndexOptionsBuilder().SetUnique(true); // page search key var keys = new IndexKeysBuilder().Ascending("ParcelName"); Parcels.EnsureIndex(keys, options); // general page search keys = new IndexKeysBuilder().Ascending("_id"); Parcels.EnsureIndex(keys, options); }
/// <summary> /// 创建索引 /// </summary> /// <param name="collection"></param> /// <param name="v"></param> private void CreateIndex(MongoCollection collection, Variant v) { var indexs = collection.GetIndexes(); HashSet<string> hs = new HashSet<string>(); foreach (IndexInfo index in indexs) { hs.Add(index.Name); } foreach (var item in v) { string indexName = item.Key; Variant keys = item.Value as Variant; if (keys != null) { IndexOptionsBuilder indexOpt = new IndexOptionsBuilder(); indexOpt.SetName(indexName); if (keys.ContainsKey("dropDups")) { if (keys["dropDups"] is bool) { indexOpt.SetDropDups((bool)keys["dropDups"]); } keys.Remove("dropDups"); } if (keys.ContainsKey("background")) { if (keys["background"] is bool) { indexOpt.SetBackground((bool)keys["background"]); } keys.Remove("background"); } if (keys.ContainsKey("unique")) { if (keys["unique"] is bool) { indexOpt.SetUnique((bool)keys["unique"]); } keys.Remove("unique"); } if (keys.ContainsKey("sparse")) { if (keys["sparse"] is bool) { indexOpt.SetSparse((bool)keys["sparse"]); } keys.Remove("sparse"); } bool find = false; IndexKeysBuilder indexKey = new IndexKeysBuilder(); foreach (var dsa in keys) { string ckey = dsa.Key; if (ckey != string.Empty && dsa.Value is int) { int dec = (int)dsa.Value; if (dec == 1) { indexKey.Ascending(ckey); find = true; } else if (dec == -1) { indexKey.Descending(ckey); find = true; } } } if (find) { if (!hs.Contains(indexName)) collection.CreateIndex(indexKey, indexOpt); } else { if (hs.Contains(indexName)) collection.DropIndexByName(indexName); } } } }
/// <summary> /// 添加索引 /// </summary> /// <param name="AscendingKey"></param> /// <param name="DescendingKey"></param> /// <param name="option"></param> /// <returns></returns> public static Boolean CreateMongoIndex(String[] AscendingKey, String[] DescendingKey, String GeoSpatialKey, IndexOptionsBuilder option) { MongoCollection mongoCol = SystemManager.GetCurrentCollection(); var indexkeys = new IndexKeysBuilder(); if (!String.IsNullOrEmpty(GeoSpatialKey)) { indexkeys.GeoSpatial(GeoSpatialKey); } indexkeys.Ascending(AscendingKey); indexkeys.Descending(DescendingKey); mongoCol.CreateIndex(indexkeys, option); return true; }
/// <summary> /// 添加索引 /// </summary> /// <param name="AscendingKey"></param> /// <param name="DescendingKey"></param> /// <param name="IsBackground"></param> /// <param name="IsDropDups"></param> /// <param name="IsSparse"></param> /// <param name="IsUnique"></param> /// <param name="IndexName"></param> /// <returns></returns> public static Boolean CreateMongoIndex(String[] AscendingKey, String[] DescendingKey, Boolean IsBackground = false, Boolean IsDropDups = false, Boolean IsSparse = false, Boolean IsUnique = false, String IndexName = "") { MongoCollection mongoCol = SystemManager.GetCurrentCollection(); IndexKeysBuilder indexkeys = new IndexKeysBuilder(); indexkeys.Ascending(AscendingKey); indexkeys.Descending(DescendingKey); IndexOptionsBuilder option = new IndexOptionsBuilder(); option.SetBackground(IsBackground); option.SetDropDups(IsDropDups); option.SetSparse(IsSparse); option.SetUnique(IsUnique); if (IndexName != String.Empty && !mongoCol.IndexExists(IndexName)) { option.SetName(IndexName); } mongoCol.CreateIndex(indexkeys, option); return true; }
public void DoCreateIndexes(OperationStatus operation, ConnectionInfo cnn, string database, string collection, IEnumerable<IndexDescriptor> indexes) { var col = MongoUtilities.Create(cnn).GetDatabase(database).GetCollection(collection); try { col.DropAllIndexes(); } catch (Exception ex) { Utilities.LogException(ex); } var count = 0; var errors = new List<string>(); foreach (var index in indexes) { operation.PercentComplete = (int) ((100.0 / (double)indexes.Count()) * (double) count); operation.Description = string.Format(Properties.Resources.ManageIndexes_Creating, ++count); try { var keys = new IndexKeysBuilder(); foreach (var property in index.IndexedProperties) { switch (property.IndexType) { case IndexType.Descending: keys.Descending(property.PropertyName); break; case IndexType.Geospatial: keys.GeoSpatial(property.PropertyName); break; default: keys.Ascending(property.PropertyName); break; } } var options = new IndexOptionsBuilder(); options.SetSparse(index.IsSparse); options.SetUnique(index.IsUnique); col.CreateIndex(keys, options); } catch (Exception ex) { if (ex.Message.Contains("2d has to be first")) { errors.Add(string.Format(Properties.Resources.ManageIndexes_GeospatialNotFirst, count)); } else if (ex.Message.Contains("geo field") || ex.Message.Contains("location object expected")) { errors.Add(string.Format(Properties.Resources.ManageIndexes_InvalidGeospatial, count)); } else if (ex.Message.Contains("duplicate key")) { errors.Add(string.Format(Properties.Resources.ManageIndexes_InvalidUnique, count)); } else { errors.Add(string.Format(Properties.Resources.ManageIndexes_UnknownError, count, ex.Message)); Utilities.LogException(ex); } } } operation.IsComplete = true; if (errors.Count == 0) { operation.IsSuccess = true; operation.Description = string.Format(Properties.Resources.ManageIndexes_Success, indexes.Count()); } else { operation.IsSuccess = false; var errorString = new StringBuilder(); errors.ToList().ForEach( error => { errorString.Append("\n"); errorString.Append(error); }); operation.Description = string.Format(Properties.Resources.ManageIndexes_Fail, errors.Count, errorString); } }
/// <summary> /// 添加索引 /// </summary> /// <param name="AscendingKey"></param> /// <param name="DescendingKey"></param> /// <param name="option"></param> /// <returns></returns> public static Boolean CreateMongoIndex(String[] AscendingKey, String[] DescendingKey, IndexOptionsBuilder option) { MongoCollection mongoCol = SystemManager.GetCurrentCollection(); IndexKeysBuilder indexkeys = new IndexKeysBuilder(); indexkeys.Ascending(AscendingKey); indexkeys.Descending(DescendingKey); mongoCol.CreateIndex(indexkeys, option); return true; }
/// <summary> /// 添加索引 /// </summary> /// <param name="ascendingKey"></param> /// <param name="descendingKey"></param> /// <param name="geoSpatialKey"></param> /// <param name="option"></param> /// <param name="currentCollection"></param> /// <returns></returns> public static bool CreateMongoIndex(string[] ascendingKey, string[] descendingKey, string geoSpatialKey, IndexOptionsBuilder option, MongoCollection currentCollection) { var mongoCol = currentCollection; var indexkeys = new IndexKeysBuilder(); if (!string.IsNullOrEmpty(geoSpatialKey)) { indexkeys.GeoSpatial(geoSpatialKey); } indexkeys.Ascending(ascendingKey); indexkeys.Descending(descendingKey); mongoCol.CreateIndex(indexkeys, option); return true; }
private void SeedShops() { MongoCollection<BsonDocument> shopsCollection = this.Database.GetCollection("Shops"); IndexKeysBuilder indexKey = new IndexKeysBuilder().Ascending("Name"); shopsCollection.CreateIndex(indexKey, IndexOptions.SetUnique(true)); if (shopsCollection.Count() == 0) { if (this.CitiesSeeds.Count == 0) { throw new MongoException("'Cities' seed collection is empty, cannot proceed with filling the 'Shops' collection."); } if (this.ProductsSeeds.Count == 0) { throw new MongoException("'Products' seed collection is empty, cannot proceed with filling the 'Shops' collection."); } ICollection<ObjectId> products = new HashSet<ObjectId>(); foreach (Product product in this.ProductsSeeds) { products.Add(product.Id); } ICollection<Shop> shopsBatch = new HashSet<Shop>() { new Shop("Store #5", "Sveti 'Golf Mk3 GTI' 5", this.CitiesSeeds[0].Id, products), new Shop("Store #6", "Musala 69", this.CitiesSeeds[2].Id, products), new Shop("Store #7", "Drujba 3", this.CitiesSeeds[1].Id, products), }; shopsCollection.InsertBatch<Shop>(shopsBatch); } }
private void CreateChangeRequestIndexes() { var keys = new IndexKeysBuilder(); keys.Ascending("Id"); var options = new IndexOptionsBuilder(); options.SetSparse(true); options.SetUnique(true); var collection = database.GetCollection<CardChange>("card_changes"); collection.EnsureIndex(keys, options); }
protected override void OnBuildIndex(IndexKeysBuilder builder) { buildCallback(builder); }
protected abstract void OnBuildIndex(IndexKeysBuilder builder);
protected override IMongoIndexKeys OnBuildIndex() { var indexKeysBuilder = new IndexKeysBuilder(); OnBuildIndex(indexKeysBuilder); return indexKeysBuilder; }
private void SeedProducts() { MongoCollection<BsonDocument> productsCollection = this.Database.GetCollection("Products"); IndexKeysBuilder indexKey = new IndexKeysBuilder().Ascending("ProductCode"); productsCollection.CreateIndex(indexKey, IndexOptions.SetUnique(true)); if (productsCollection.Count() == 0) { if (this.ProductTypesSeeds.Count == 0) { throw new MongoException("'ProductTypes' seed collection is empty, cannot proceed with filling the 'Products' collection."); } // Category IDs correspond to MS SQL table 'Categories' this.ProductsSeeds = new Product[] { new Product("O-Tight", "Giving her first time experience", 1501, 14.99M, 100, this.ProductTypesSeeds[1].Id, new[] { 2 }), new Product("Bull Performance", "-", 1502, 19.99M, 59, this.ProductTypesSeeds[2].Id, new[] { 1 }), new Product("Leather Whip", "Pervert's must-have tool", 1503, 46.50M, 11, this.ProductTypesSeeds[0].Id, new[] { 3 }), new Product("B(.)(.)Bster Whip", "Special leather whip", 1504, 149.99M, 25, this.ProductTypesSeeds[0].Id, new[] { 2 }) }; productsCollection.InsertBatch<Product>(this.ProductsSeeds); } }
private void CreateUserCardIndexes() { var keys = new IndexKeysBuilder(); keys.Ascending("PlaneswalkerId","MultiverseId"); var options = new IndexOptionsBuilder(); options.SetSparse(true); options.SetUnique(true); var collection = database.GetCollection<UserCard>("user_cards"); collection.EnsureIndex(keys, options); }
/// <summary> /// 添加索引 /// </summary> /// <param name="IdxOpt"></param> /// <param name="option"></param> /// <param name="currentCollection"></param> /// <returns></returns> public static bool CreateMongoIndex(IndexOption IdxOpt, IndexOptionsBuilder option, MongoCollection currentCollection,ref string errorMessage) { var mongoCol = currentCollection; var indexkeys = new IndexKeysBuilder(); if (!string.IsNullOrEmpty(IdxOpt.GeoSpatialHaystackKey)) indexkeys.GeoSpatialHaystack(IdxOpt.GeoSpatialHaystackKey); if (!string.IsNullOrEmpty(IdxOpt.GeoSpatialKey)) indexkeys.GeoSpatial(IdxOpt.GeoSpatialKey); if (!string.IsNullOrEmpty(IdxOpt.GeoSpatialSphericalKey)) indexkeys.GeoSpatialSpherical(IdxOpt.GeoSpatialSphericalKey); if (!string.IsNullOrEmpty(IdxOpt.HashedKey)) indexkeys.Hashed(IdxOpt.HashedKey); indexkeys.Ascending(IdxOpt.AscendingKey.ToArray()); indexkeys.Descending(IdxOpt.DescendingKey.ToArray()); indexkeys.Text(IdxOpt.TextKey.ToArray()); //CreateIndex失败的时候会出现异常! try { var result = mongoCol.CreateIndex(indexkeys, option); return result.Response.GetElement("ok").Value.AsInt32 == 1; } catch (Exception ex) { errorMessage = ex.ToString(); return false; } }
/// <summary> /// Initializes all the needed indexes for the pages to optimize search on them. /// </summary> public void InitializeIndexes() { // key options var options = new IndexOptionsBuilder().SetUnique(true); // page search key //var keys = new IndexKeysBuilder().Ascending("Spot"); //Spots.EnsureIndex(keys, options); // general page search var keys = new IndexKeysBuilder().Ascending("_id"); Spots.EnsureIndex(keys, options); options.SetUnique(false); keys = new IndexKeysBuilder().Ascending("PhaseID"); Spots.EnsureIndex(keys, options); }
private void SeedProductType() { MongoCollection<BsonDocument> productTypesCollection = this.Database.GetCollection("ProductTypes"); IndexKeysBuilder indexKey = new IndexKeysBuilder().Ascending("Name"); productTypesCollection.CreateIndex(indexKey, IndexOptions.SetUnique(true)); if (productTypesCollection.Count() == 0) { this.ProductTypesSeeds = new ProductType[] { new ProductType("Whip"), new ProductType("Shrink V****a Pills"), new ProductType("Stimulants") }; productTypesCollection.InsertBatch<ProductType>(this.ProductTypesSeeds); } }