示例#1
0
 public MongoIndexKeysWarpper GeoSpatialHaystack(string name)
 {
     if (MongoIndexKeys == null)
     {
         MongoIndexKeys = IndexKeys.GeoSpatialHaystack(name);
     }
     else
     {
         MongoIndexKeys = MongoIndexKeys.GeoSpatialHaystack(name);
     }
     return(this);
 }
示例#2
0
        /// <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);
            }
        }
示例#3
0
        private void CreateIndexBasicMode()
        {
            string indexName = txtBoxIndexName.Text;

            if (string.IsNullOrWhiteSpace(indexName))
            {
                throw new Exception("Please enter an index name.");
            }

            var mongoCollection = MongoCollectionInfo.GetMongoCollection();
            var indexes         = mongoCollection.GetIndexes();

            if (indexes != null && indexes.Any())
            {
                if (indexes.ToList().Exists(i => i.Name == indexName))
                {
                    throw new Exception("An index with that name already exists.");
                }
            }

            var keys = GetChoosenKeys();

            if (!keys.Any())
            {
                throw new Exception("You must choose at least one key.");
            }

            var keyBuilder = new IndexKeysBuilder();

            foreach (var key in keys)
            {
                if (key.SortType == 1)
                {
                    keyBuilder = keyBuilder.Ascending(key.Key);
                }
                else if (key.SortType == -1)
                {
                    keyBuilder = keyBuilder.Descending(key.Key);
                }
                else if (key.SortType == 2)
                {
                    keyBuilder = keyBuilder.GeoSpatial(key.Key);
                }
                else if (key.SortType == 3)
                {
                    keyBuilder = keyBuilder.GeoSpatialHaystack(key.Key);
                }
            }
            var optionBuilder = new IndexOptionsBuilder();

            optionBuilder.SetUnique(checkBoxUnique.Checked);
            optionBuilder.SetBackground(checkBoxBackground.Checked);
            optionBuilder.SetDropDups(checkBoxUnique.Checked && checkBoxDropDups.Checked);
            optionBuilder.SetSparse(checkBoxSparse.Checked);
            optionBuilder.SetName(indexName);

            var writeConcernResult = mongoCollection.CreateIndex(keyBuilder, optionBuilder);

            if (writeConcernResult.HasLastErrorMessage)
            {
                throw new Exception(writeConcernResult.LastErrorMessage);
            }
        }