/// <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="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; } }