/// <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 Text(params string[] names) { if (MongoIndexKeys == null) { MongoIndexKeys = IndexKeys.Text(names); } else { MongoIndexKeys = MongoIndexKeys.Text(names); } return(this); }
/// <summary> /// 设置Text索引 /// </summary> /// <param name="collectionName"></param> /// <param name="FieldName"></param> /// <param name="database"></param> public static void SetTextIndex(string collectionName, string FieldName, string database = "") { if (string.IsNullOrEmpty(database)) { database = _defaultDatabaseName; } MongoCollection col = GetDatabaseByType(database).GetCollection(collectionName); if (col.IndexExistsByName(FieldName)) { return; } var option = new IndexOptionsBuilder(); option.SetName(FieldName); var indexkeys = new IndexKeysBuilder(); indexkeys.Text(new string[] { FieldName }); col.CreateIndex(indexkeys, option); }