/// <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); } 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); } }
public MongoIndexKeysWarpper GeoSpatial(string name) { if (MongoIndexKeys == null) { MongoIndexKeys = IndexKeys.GeoSpatial(name); } else { MongoIndexKeys = MongoIndexKeys.GeoSpatial(name); } return(this); }
/// <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(); IndexKeysBuilder indexkeys = new IndexKeysBuilder(); if (!String.IsNullOrEmpty(GeoSpatialKey)) { indexkeys.GeoSpatial(GeoSpatialKey); } indexkeys.Ascending(AscendingKey); indexkeys.Descending(DescendingKey); mongoCol.EnsureIndex(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 CreateIndexBasicMode() { string indexName = txtBoxIndexName.Text; if (string.IsNullOrWhiteSpace(indexName)) { throw new Exception("Please enter an index name."); } var mongoCollection = MongoCollectionInfo.GetMongoCollection(); var indexes = mongoCollection.GetIndexes(); if (indexes != null && indexes.Any()) { if (indexes.ToList().Exists(i => i.Name == indexName)) { throw new Exception("An index with that name already exists."); } } var keys = GetChoosenKeys(); if (!keys.Any()) { throw new Exception("You must choose at least one key."); } var keyBuilder = new IndexKeysBuilder(); foreach (var key in keys) { if (key.SortType == 1) { keyBuilder = keyBuilder.Ascending(key.Key); } else if (key.SortType == -1) { keyBuilder = keyBuilder.Descending(key.Key); } else if (key.SortType == 2) { keyBuilder = keyBuilder.GeoSpatial(key.Key); } else if (key.SortType == 3) { keyBuilder = keyBuilder.GeoSpatialHaystack(key.Key); } } var optionBuilder = new IndexOptionsBuilder(); optionBuilder.SetUnique(checkBoxUnique.Checked); optionBuilder.SetBackground(checkBoxBackground.Checked); optionBuilder.SetDropDups(checkBoxUnique.Checked && checkBoxDropDups.Checked); optionBuilder.SetSparse(checkBoxSparse.Checked); optionBuilder.SetName(indexName); var writeConcernResult = mongoCollection.CreateIndex(keyBuilder, optionBuilder); if (writeConcernResult.HasLastErrorMessage) { throw new Exception(writeConcernResult.LastErrorMessage); } }