Exemplo n.º 1
0
        public string Cmd()
        {
            var mongo   = new MongoDBTool().GetMongoCollection <TestTTL>();
            var options = new CreateIndexOptions <TestTTL>();
            var be      = new BsonElement("expireAfterSeconds", 20);

            //options.StorageEngine = new BsonDocument { { "fsdfdsfd", 20 } };
            //options.Weights = new BsonDocument { { "expireAfterSeconds", 20 } };
            options.Name    = "testIndex";
            options.Version = 1;
            //mongo.Indexes.CreateOne(new BsonDocument() { { "CreateTime", 1 } } ,options);
            var bd = new BsonDocument()
            {
                { "CreateTime", 1 }
            };
            var idk = Builders <TestTTL> .IndexKeys;

            //idk.Combine();
            idk.Ascending(x => x.CreateTime);
            var iod = new IndexOptionDefaults();

            options.ExpireAfter = new TimeSpan(0, 0, 20);
            mongo.Indexes.CreateOne(new BsonDocument()
            {
                { "CreateTime", 1 }
            }, options);
            mongo.InsertOne(new TestTTL()
            {
                CreateTime = DateTime.Now, Code = "fsdfsdf"
            });
            return("sss");
        }
        public void TestCreateCollectionSetIndexOptionDefaults()
        {
            var collection = _database.GetCollection("testindexoptiondefaults");

            collection.Drop();
            Assert.IsFalse(collection.Exists());
            var storageEngineOptions = new BsonDocument("mmapv1", new BsonDocument());
            var indexOptionDefaults  = new IndexOptionDefaults {
                StorageEngine = storageEngineOptions
            };
            var expectedIndexOptionDefaultsDocument = new BsonDocument("storageEngine", storageEngineOptions);
            var options = CollectionOptions.SetIndexOptionDefaults(indexOptionDefaults);

            _database.CreateCollection(collection.Name, options);

            var commandResult  = _database.RunCommand("listCollections");
            var collectionInfo = commandResult.Response["cursor"]["firstBatch"].AsBsonArray.Where(doc => doc["name"] == collection.Name).Single().AsBsonDocument;

            Assert.AreEqual(expectedIndexOptionDefaultsDocument, collectionInfo["options"]["indexOptionDefaults"]);
        }
        public void TestCreateCollectionSetIndexOptionDefaults()
        {
            RequireServer.Check().Supports(Feature.IndexOptionsDefaults).ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet);
            var collection = _database.GetCollection("testindexoptiondefaults");

            collection.Drop();
            Assert.False(collection.Exists());
            var storageEngine        = CoreTestConfiguration.GetStorageEngine();
            var storageEngineOptions = new BsonDocument(storageEngine, new BsonDocument());
            var indexOptionDefaults  = new IndexOptionDefaults {
                StorageEngine = storageEngineOptions
            };
            var expectedIndexOptionDefaultsDocument = new BsonDocument("storageEngine", storageEngineOptions);
            var options = CollectionOptions.SetIndexOptionDefaults(indexOptionDefaults);

            _database.CreateCollection(collection.Name, options);

            var commandResult  = _database.RunCommand("listCollections");
            var collectionInfo = commandResult.Response["cursor"]["firstBatch"].AsBsonArray.Where(doc => doc["name"] == collection.Name).Single().AsBsonDocument;

            Assert.Equal(expectedIndexOptionDefaultsDocument, collectionInfo["options"]["indexOptionDefaults"]);
        }
 /// <summary>
 /// Sets the index options defaults.
 /// </summary>
 /// <param name="value">The value.</param>
 /// <returns>The builder (so method calls can be chained).</returns>
 public static CollectionOptionsBuilder SetIndexOptionDefaults(IndexOptionDefaults value)
 {
     return(new CollectionOptionsBuilder().SetIndexOptionDefaults(value));
 }
 /// <summary>
 /// Sets the index options defaults.
 /// </summary>
 /// <param name="value">The index options defaults.</param>
 /// <returns>The builder (so method calls can be chained).</returns>
 public CollectionOptionsBuilder SetIndexOptionDefaults(IndexOptionDefaults value)
 {
     _document["indexOptionDefaults"] = value.ToBsonDocument();
     return(this);
 }