/// <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) { List <String> AscendingKey = new List <String>(); List <String> DescendingKey = new List <String>(); String GeoSpatialKey = string.Empty; String FirstKey = string.Empty; String TextKey = String.Empty; for (int i = 0; i < 5; i++) { ctlIndexCreate ctl = (ctlIndexCreate)Controls.Find("ctlIndexCreate" + (i + 1).ToString(), true)[0]; if (ctl.KeyName != String.Empty) { 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; } } } IndexOptionsBuilder 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." + System.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)) { IndexKeysDocument TextKeysDoc = new IndexKeysDocument(); TextKeysDoc.Add(TextKey, "text"); SystemManager.GetCurrentCollection().EnsureIndex(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."); } }