Beispiel #1
0
        public MongoIndexKeysWarpper Hashed(string name)
        {
            if (MongoIndexKeys == null)
            {
                MongoIndexKeys = IndexKeys.Hashed(name);
            }
            else
            {
                MongoIndexKeys = MongoIndexKeys.Hashed(name);
            }

            return(this);
        }
Beispiel #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);
            }
            if (!string.IsNullOrEmpty(IdxOpt.HashedKey))
            {
                indexkeys.Hashed(IdxOpt.HashedKey);
            }
            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);
            }
        }