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> /// 添加索引 /// </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; }
/// <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="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; }
/// <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="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; }
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="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; } }