internal static void InitializeDB() { var keys = new IndexKeysDocument(); keys.Add(new BsonElement("User", 1)); keys.Add(new BsonElement("Day", -1)); Entries.EnsureIndex(keys); }
private IndexKeysDocument ToDoc(List<KeyModel> keys) { var doc = new IndexKeysDocument(); if (keys != null && keys.Count > 0) { foreach (var key in keys) { doc.Add(key.Field, key.Order); } } return doc; }
/// <summary> /// Ensure index for Get() method /// </summary> /// <param name="beforeSort">fields in Get() call that should be before the sort fields in the index</param> /// <param name="afterSort">fields in Get() call that should be after the sort fields in the index</param> /// <exception cref="ArgumentNullException">beforeSort or afterSort is null</exception> /// <exception cref="ArgumentException">beforeSort or afterSort field value is not 1 or -1</exception> public void EnsureGetIndex(IndexKeysDocument beforeSort, IndexKeysDocument afterSort) { if (beforeSort == null) throw new ArgumentNullException("beforeSort"); if (afterSort == null) throw new ArgumentNullException("afterSort"); //using general rule: equality, sort, range or more equality tests in that order for index var completeIndex = new IndexKeysDocument("running", 1); foreach (var field in beforeSort) { if (field.Value != 1 && field.Value != -1) throw new ArgumentException("field values must be 1 or -1 for ascending or descending", "beforeSort"); completeIndex.Add("payload." + field.Name, field.Value); } completeIndex.Add("priority", 1); completeIndex.Add("created", 1); foreach (var field in afterSort) { if (field.Value != 1 && field.Value != -1) throw new ArgumentException("field values must be 1 or -1 for ascending or descending", "afterSort"); completeIndex.Add("payload." + field.Name, field.Value); } completeIndex.Add("earliestGet", 1); EnsureIndex(completeIndex);//main query in Get() EnsureIndex(new IndexKeysDocument { { "running", 1 }, { "resetTimestamp", 1 } });//for the stuck messages query in Get() }
/// <summary> /// Ensure index for Count() method /// </summary> /// <param name="index">fields in Count() call</param> /// <param name="includeRunning">whether running was given to Count() or not</param> /// <exception cref="ArgumentNullException">index was null</exception> /// <exception cref="ArgumentException">index field value is not 1 or -1</exception> public void EnsureCountIndex(IndexKeysDocument index, bool includeRunning) { if (index == null) throw new ArgumentNullException("index"); var completeFields = new IndexKeysDocument(); if (includeRunning) completeFields.Add("running", 1); foreach (var field in index) { if (field.Value != 1 && field.Value != -1) throw new ArgumentException("field values must be 1 or -1 for ascending or descending", "index"); completeFields.Add("payload." + field.Name, field.Value); } EnsureIndex(completeFields); }
/// <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."); } }