Esempio n. 1
0
        /// <summary>
        /// EnsureIndex for TTL(Time To Live) - Auto expiration feature
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void EnsureTTLIndex <T>() where T : class, new()
        {
            IndexKeysDocument    ikd = new IndexKeysDocument(new BsonElement("Date", 1));
            IndexOptionsDocument iod = new IndexOptionsDocument(new BsonElement("expireAfterSeconds", 21600)); // 6 hours

            mongoDb.GetCollection <T>(GetTypeName(typeof(T))).CreateIndex(ikd, iod);
        }
        public void TestIndexOptionsDocumentConstructor()
        {
            var document1 = new IndexOptionsDocument(_dictionary);
            var document2 = new IndexOptionsDocument(_hashtable);
            var document3 = new IndexOptionsDocument(_idictionaryNonGeneric);
            var document4 = new IndexOptionsDocument(_idictionary);

            Assert.AreEqual("Dictionary<string, object>", document1["type"].AsString);
            Assert.AreEqual("Hashtable", document2["type"].AsString);
            Assert.AreEqual("IDictionary", document3["type"].AsString);
            Assert.AreEqual("IDictionary<string, object>", document4["type"].AsString);
        }
        /// <summary>
        /// 新建索引
        /// </summary>
        /// <param name="jsonData"></param>
        public void CreateIndex(string jsonData)
        {
            var model     = JsonConvert.DeserializeObject <SaveIndexModel>(jsonData);
            var idxDoc    = ToDoc(model.Keys);
            var idxOption = new IndexOptionsDocument();

            idxOption.Add("background", model.Background);
            idxOption.Add("dropdups", model.Dropdups);

            var mongo  = new MongoClient(string.Format(MongoConst.ConnString, Server.Name));
            var server = mongo.GetServer();
            var db     = server.GetDatabase(Database.Name);

            var tbl = db.GetCollection(Table.Name);
            var rst = tbl.CreateIndex(idxDoc, idxOption);

            MongoCache.Clear();
        }
Esempio n. 4
0
        private void CreateIndexJsonMode()
        {
            string indexJson = textBoxIndexJson.Text.Trim();

            if (string.IsNullOrWhiteSpace(indexJson))
            {
                throw new Exception("Enter index json.");
            }

            IndexKeysDocument keys = null;

            try
            {
                keys = BsonSerializer.Deserialize <IndexKeysDocument>(indexJson);
            }
            catch (Exception ex1)
            {
                throw new Exception($"Invalid index json.\r\n\r\n{ex1.Message}");
            }

            string optionJson            = textBoxIndexOptionsJson.Text.Trim();
            IndexOptionsDocument options = null;

            if (string.IsNullOrWhiteSpace(optionJson))
            {
                options = new IndexOptionsDocument();
            }
            else
            {
                try
                {
                    options = BsonSerializer.Deserialize <IndexOptionsDocument>(optionJson);
                }
                catch (Exception ex1)
                {
                    throw new Exception($"Invalid options json.\r\n\r\n{ex1.Message}");
                }
            }

            if (!options.Contains("name"))
            {
                string indexName = txtBoxIndexName.Text;
                if (string.IsNullOrWhiteSpace(indexName))
                {
                    throw new Exception("Please enter an index name.");
                }

                options.Add("name", indexName);
            }

            if (!options.Contains("unique"))
            {
                options.Add("unique", checkBoxUnique.Checked);
            }
            if (!options.Contains("dropDups") && checkBoxUnique.Checked)
            {
                options.Add("dropDups", checkBoxDropDups.Checked);
            }
            if (!options.Contains("background"))
            {
                options.Add("background", checkBoxBackground.Checked);
            }
            if (!options.Contains("sparse"))
            {
                options.Add("sparse", checkBoxSparse.Checked);
            }

            var mongoCollection = MongoCollectionInfo.GetMongoCollection();
            WriteConcernResult writeConcernResult = mongoCollection.CreateIndex(keys, options);

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