public GeoSpatial ( string name ) : |
||
name | string | The key name. |
리턴 |
/// <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; }
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, 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="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; } }