public SetName ( string value ) : |
||
value | string | The name of the index. |
Résultat |
/// <summary> /// 增加索引 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmdAddIndex_Click(object sender, EventArgs e) { List<String> AscendingKey = new List<String>(); List<String> DescendingKey = new List<String>(); for (int i = 0; i < 5; i++) { ctlIndexCreate ctl = (ctlIndexCreate)Controls.Find("ctlIndexCreate" + (i + 1).ToString(), true)[0]; if (ctl.KeyName != String.Empty) { if (ctl.IsAscendingKey) { AscendingKey.Add(ctl.KeyName); } else { DescendingKey.Add(ctl.KeyName); } } } IndexOptionsBuilder option = new IndexOptionsBuilder(); option.SetBackground(chkIsBackground.Checked); option.SetDropDups(chkIsDroppedDups.Checked); option.SetSparse(chkIsSparse.Checked); option.SetUnique(chkIsUnique.Checked); if (txtIndexName.Text != String.Empty && !SystemManager.GetCurrentCollection().IndexExists(txtIndexName.Text)) { option.SetName(txtIndexName.Text); } MongoDBHelper.CreateMongoIndex(AscendingKey.ToArray(), DescendingKey.ToArray(), option); RefreshList(); MessageBox.Show("Index Add Completed!"); }
/// <summary> /// 增加索引 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmdAddIndex_Click(object sender, EventArgs e) { var AscendingKey = new List<String>(); var DescendingKey = new List<String>(); String GeoSpatialKey = string.Empty; String FirstKey = string.Empty; String TextKey = String.Empty; for (int i = 0; i < 5; i++) { var ctl = (ctlIndexCreate) Controls.Find("ctlIndexCreate" + (i + 1), true)[0]; if (ctl.KeyName == String.Empty) continue; FirstKey = ctl.KeyName.Trim(); switch (ctl.IndexKeyType) { case MongoDbHelper.IndexType.Ascending: AscendingKey.Add(ctl.KeyName.Trim()); break; case MongoDbHelper.IndexType.Descending: DescendingKey.Add(ctl.KeyName.Trim()); break; case MongoDbHelper.IndexType.GeoSpatial: GeoSpatialKey = ctl.KeyName.Trim(); break; case MongoDbHelper.IndexType.Text: TextKey = ctl.KeyName.Trim(); break; default: break; } } var option = new IndexOptionsBuilder(); option.SetBackground(chkIsBackground.Checked); option.SetDropDups(chkIsDroppedDups.Checked); option.SetSparse(chkIsSparse.Checked); option.SetUnique(chkIsUnique.Checked); if (chkExpireData.Checked) { //TTL的限制条件很多 //http://docs.mongodb.org/manual/tutorial/expire-data/ //不能是组合键 Boolean CanUseTTL = true; if ((AscendingKey.Count + DescendingKey.Count + (String.IsNullOrEmpty(GeoSpatialKey) ? 0 : 1)) != 1) { MyMessageBox.ShowMessage("Can't Set TTL", "the TTL index may not be compound (may not have multiple fields)."); CanUseTTL = false; } else { //不能是_id if (FirstKey == MongoDbHelper.KEY_ID) { MyMessageBox.ShowMessage("Can't Set TTL", "you cannot create this index on the _id field, or a field that already has an index."); CanUseTTL = false; } } if (SystemManager.GetCurrentCollection().IsCapped()) { MyMessageBox.ShowMessage("Can't Set TTL", "you cannot use a TTL index on a capped collection, because MongoDB cannot remove documents from a capped collection."); CanUseTTL = false; } if (CanUseTTL) { MyMessageBox.ShowMessage("Constraints", "Constraints Of TimeToLive", "the indexed field must be a date BSON type. If the field does not have a date type, the data will not expire." + Environment.NewLine + "if the field holds an array, and there are multiple date-typed data in the index, the document will expire when the lowest (i.e. earliest) matches the expiration threshold.", true); option.SetTimeToLive(new TimeSpan(0, 0, (int) numTTL.Value)); } } if (txtIndexName.Text != String.Empty && !SystemManager.GetCurrentCollection().IndexExists(txtIndexName.Text) && (AscendingKey.Count + DescendingKey.Count + (String.IsNullOrEmpty(GeoSpatialKey) ? 0 : 1) + (String.IsNullOrEmpty(TextKey) ? 0 : 1)) != 0) { option.SetName(txtIndexName.Text); try { //暂时要求只能一个TextKey if (!string.IsNullOrEmpty(TextKey)) { var TextKeysDoc = new IndexKeysDocument {{TextKey, "text"}}; SystemManager.GetCurrentCollection().CreateIndex(TextKeysDoc, option); } else { MongoDbHelper.CreateMongoIndex(AscendingKey.ToArray(), DescendingKey.ToArray(), GeoSpatialKey, option); } MyMessageBox.ShowMessage("Index Add Completed!", "IndexName:" + txtIndexName.Text + " is add to collection."); } catch (Exception ex) { SystemManager.ExceptionDeal(ex, "Index Add Failed!", "IndexName:" + txtIndexName.Text); } RefreshList(); } else { MyMessageBox.ShowMessage("Index Add Failed!", "Please Check the index information."); } }
/// <summary> /// /// </summary> /// <param name="uiOption"></param> /// <param name="strMessageTitle"></param> /// <param name="strMessageContent"></param> /// <returns></returns> public static bool CreateIndex(IndexOption uiOption, ref string strMessageTitle, ref string strMessageContent) { var result = true; var option = new IndexOptionsBuilder(); option.SetBackground(uiOption.IsBackground); option.SetDropDups(uiOption.IsDropDups); option.SetSparse(uiOption.IsSparse); option.SetUnique(uiOption.IsUnique); if (uiOption.IsPartial) { IMongoQuery query = (QueryDocument) BsonDocument.Parse(uiOption.PartialCondition); option.SetPartialFilterExpression(query); } if (uiOption.IsExpireData) { //TTL的限制条件很多 //http://docs.mongodb.org/manual/tutorial/expire-data/ //不能是组合键 var canUseTtl = true; if (uiOption.AscendingKey.Count + uiOption.DescendingKey.Count + (string.IsNullOrEmpty(uiOption.GeoSpatialKey) ? 0 : 1) != 1) { strMessageTitle = "Can't Set TTL"; strMessageContent = "the TTL index may not be compound (may not have multiple fields)."; canUseTtl = false; } else { //不能是_id if (uiOption.FirstKey == ConstMgr.KeyId) { strMessageTitle = "Can't Set TTL"; strMessageContent = "you cannot create this index on the _id field, or a field that already has an index."; canUseTtl = false; } } if (RuntimeMongoDbContext.GetCurrentCollection().IsCapped()) { strMessageTitle = "Can't Set TTL"; strMessageContent = "you cannot use a TTL index on a capped collection, because MongoDB cannot remove documents from a capped collection."; canUseTtl = false; } if (canUseTtl) { strMessageTitle = "Constraints Of TimeToLive"; strMessageContent = "the indexed field must be a date BSON type. If the field does not have a date type, the data will not expire." + Environment.NewLine + "if the field holds an array, and there are multiple date-typed data in the index, the document will expire when the lowest (i.e. earliest) matches the expiration threshold."; option.SetTimeToLive(new TimeSpan(0, 0, uiOption.Ttl)); } } var totalIndex = uiOption.AscendingKey.Count + uiOption.DescendingKey.Count + (string.IsNullOrEmpty(uiOption.GeoSpatialKey) ? 0 : 1) + (string.IsNullOrEmpty(uiOption.TextKey) ? 0 : 1); if (uiOption.IndexName != string.Empty && !RuntimeMongoDbContext.GetCurrentCollection().IndexExists(uiOption.IndexName) && totalIndex != 0) { option.SetName(uiOption.IndexName); try { //暂时要求只能一个TextKey if (!string.IsNullOrEmpty(uiOption.TextKey)) { var textKeysDoc = new IndexKeysDocument {{uiOption.TextKey, "text"}}; RuntimeMongoDbContext.GetCurrentCollection().CreateIndex(textKeysDoc, option); } else { CreateMongoIndex(uiOption.AscendingKey.ToArray(), uiOption.DescendingKey.ToArray(), uiOption.GeoSpatialKey, option, RuntimeMongoDbContext.GetCurrentCollection()); } strMessageTitle = "Index Add Completed!"; strMessageContent = "IndexName:" + uiOption.IndexName + " is add to collection."; } catch { strMessageTitle = "Index Add Failed!"; strMessageContent = "IndexName:" + uiOption.IndexName; result = false; } } else { strMessageTitle = "Index Add Failed!"; strMessageContent = "Please Check the index information."; result = false; } return result; }
/// <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="KeyOptions"></param> /// <param name="strMessageTitle"></param> /// <param name="strMessageContent"></param> /// <returns></returns> public static bool CreateIndex(IndexOption KeyOptions, ref string strMessageTitle, ref string strMessageContent) { var option = new IndexOptionsBuilder(); option.SetBackground(KeyOptions.IsBackground); option.SetDropDups(KeyOptions.IsDropDups); option.SetSparse(KeyOptions.IsSparse); option.SetUnique(KeyOptions.IsUnique); if (KeyOptions.IsPartial) { IMongoQuery query = (QueryDocument)BsonDocument.Parse(KeyOptions.PartialCondition); option.SetPartialFilterExpression(query); } if (KeyOptions.IsExpireData) { //TTL的限制条件很多 //http://docs.mongodb.org/manual/tutorial/expire-data/ //不能是组合键 var canUseTtl = true; if (KeyOptions.AscendingKey.Count + KeyOptions.DescendingKey.Count + (string.IsNullOrEmpty(KeyOptions.GeoSpatialKey) ? 0 : 1) != 1) { strMessageTitle = "Can't Set TTL"; strMessageContent = "the TTL index may not be compound (may not have multiple fields)."; canUseTtl = false; } else { //不能是_id if (KeyOptions.FirstKey == ConstMgr.KeyId) { strMessageTitle = "Can't Set TTL"; strMessageContent = "you cannot create this index on the _id field, or a field that already has an index."; canUseTtl = false; } } if (RuntimeMongoDbContext.GetCurrentCollection().IsCapped()) { strMessageTitle = "Can't Set TTL"; strMessageContent = "you cannot use a TTL index on a capped collection, because MongoDB cannot remove documents from a capped collection."; canUseTtl = false; } if (canUseTtl) { strMessageTitle = "Constraints Of TimeToLive"; strMessageContent = "the indexed field must be a date BSON type. If the field does not have a date type, the data will not expire." + Environment.NewLine + "if the field holds an array, and there are multiple date-typed data in the index, the document will expire when the lowest (i.e. earliest) matches the expiration threshold."; option.SetTimeToLive(new TimeSpan(0, 0, KeyOptions.Ttl)); } } var totalIndex = KeyOptions.AscendingKey.Count + KeyOptions.DescendingKey.Count + KeyOptions.TextKey.Count; totalIndex += string.IsNullOrEmpty(KeyOptions.GeoSpatialHaystackKey) ? 0 : 1; totalIndex += string.IsNullOrEmpty(KeyOptions.GeoSpatialKey) ? 0 : 1; totalIndex += string.IsNullOrEmpty(KeyOptions.GeoSpatialSphericalKey) ? 0 : 1; totalIndex += string.IsNullOrEmpty(KeyOptions.HashedKey) ? 0 : 1; if (string.IsNullOrEmpty(KeyOptions.IndexName) || totalIndex == 0 || RuntimeMongoDbContext.GetCurrentCollection().IndexExists(KeyOptions.IndexName)) { strMessageTitle = "Index Add Failed!"; strMessageContent = "Please Check the index information."; return false; } option.SetName(KeyOptions.IndexName); string errorMessage = string.Empty; if (CreateMongoIndex(KeyOptions, option, RuntimeMongoDbContext.GetCurrentCollection(), ref errorMessage)) { strMessageTitle = "Index Add Completed!"; strMessageContent = "IndexName:" + KeyOptions.IndexName + " is add to collection."; return true; } else { strMessageTitle = "Index Add Failed!"; strMessageContent = errorMessage; return false; } }