public void TestName() { var options = IndexOptions.SetName("custom"); string expected = "{ \"name\" : \"custom\" }"; Assert.Equal(expected, options.ToJson()); }
public void TestNameBackground() { var options = IndexOptions.SetName("custom").SetBackground(true); string expected = "{ \"name\" : \"custom\", \"background\" : true }"; Assert.Equal(expected, options.ToJson()); }
public void TestNameUnique() { var options = IndexOptions.SetName("custom").SetUnique(true); string expected = "{ \"name\" : \"custom\", \"unique\" : true }"; Assert.Equal(expected, options.ToJson()); }
public void TestTextOptions() { var options = IndexOptions.SetName("custom").SetTextDefaultLanguage("spanish").SetTextLanguageOverride("idioma").SetWeight("a", 2); string expected = "{ \"name\" : \"custom\", \"default_language\" : \"spanish\", \"language_override\" : \"idioma\", \"weights\" : { \"a\" : 2 } }"; Assert.Equal(expected, options.ToJson()); }
public void TestNameGeoSpatialRange() { var options = IndexOptions.SetName("custom").SetGeoSpatialRange(1.1, 2.2); string expected = "{ \"name\" : \"custom\", \"min\" : 1.1, \"max\" : 2.2 }"; Assert.Equal(expected, options.ToJson()); }
public void CreateIndex <T>(string name, bool isUnique, params string[] keys) { var options = IndexOptions.SetName(name).SetUnique(isUnique); var indexKeys = IndexKeys.Descending(keys); db.GetCollection <T>().EnsureIndex(indexKeys, options); }
public virtual void Initialize() { if (Interlocked.Increment(ref this.initialized) > 1) { return; } Logger.Debug(Messages.InitializingStorage); this.TryMongo(() => { this.PersistedCommits.EnsureIndex( IndexKeys.Ascending("Dispatched").Ascending("CommitStamp"), IndexOptions.SetName("Dispatched_Index").SetUnique(false)); this.PersistedCommits.EnsureIndex( IndexKeys.Ascending("_id.StreamId", "Events.StreamRevision"), IndexOptions.SetName("GetFrom_Index").SetUnique(true)); this.PersistedCommits.EnsureIndex( IndexKeys.Ascending("CommitStamp"), IndexOptions.SetName("CommitStamp_Index").SetUnique(false)); this.PersistedStreamHeads.EnsureIndex( IndexKeys.Ascending("Unsnapshotted"), IndexOptions.SetName("Unsnapshotted_Index").SetUnique(false)); }); }
public void TestNameDropDups() { var options = IndexOptions.SetName("custom").SetDropDups(true); string expected = "{ \"name\" : \"custom\", \"dropDups\" : true }"; Assert.Equal(expected, options.ToJson()); }
public virtual void Initialize() { if (Interlocked.Increment(ref _initialized) > 1) { return; } Logger.Debug(Messages.InitializingStorage); TryMongo(() => { PersistedCommits.EnsureIndex(IndexKeys.Ascending("Dispatched").Ascending(MongoFields.CommitStamp), IndexOptions.SetName("Dispatched_Index").SetUnique(false)); PersistedCommits.EnsureIndex(IndexKeys.Ascending("_id.BucketId", "_id.StreamId", "Events.StreamRevision"), IndexOptions.SetName("GetFrom_Index").SetUnique(true)); PersistedCommits.EnsureIndex(IndexKeys.Ascending(MongoFields.CommitStamp), IndexOptions.SetName("CommitStamp_Index").SetUnique(false)); PersistedStreamHeads.EnsureIndex(IndexKeys.Ascending("Unsnapshotted"), IndexOptions.SetName("Unsnapshotted_Index").SetUnique(false)); IMongoQuery query = Query.EQ("_id", MongoFields.CheckpointNumber); IMongoUpdate update = Update.Replace(new BsonDocument { { "_id", "CheckpointNumber" }, { "seq", 0 } }); Counters.Update(query, update, UpdateFlags.Upsert, WriteConcern.Acknowledged); }); }
public virtual void Initialize() { if (Interlocked.Increment(ref _initialized) > 1) { return; } Logger.Debug(Messages.InitializingStorage); TryMongo(() => { PersistedCommits.CreateIndex( IndexKeys .Ascending(MongoCommitFields.Dispatched) .Ascending(MongoCommitFields.CommitStamp), IndexOptions.SetName(MongoCommitIndexes.Dispatched).SetUnique(false) ); PersistedCommits.CreateIndex( IndexKeys.Ascending( MongoCommitFields.BucketId, MongoCommitFields.StreamId, MongoCommitFields.StreamRevisionFrom, MongoCommitFields.StreamRevisionTo //,MongoCommitFields.FullqualifiedStreamRevision ), IndexOptions.SetName(MongoCommitIndexes.GetFrom).SetUnique(true) ); PersistedCommits.CreateIndex( IndexKeys.Ascending( MongoCommitFields.BucketId, MongoCommitFields.StreamId, MongoCommitFields.CommitSequence ), IndexOptions.SetName(MongoCommitIndexes.LogicalKey).SetUnique(true) ); PersistedCommits.CreateIndex( IndexKeys.Ascending(MongoCommitFields.CommitStamp), IndexOptions.SetName(MongoCommitIndexes.CommitStamp).SetUnique(false) ); PersistedStreamHeads.CreateIndex( IndexKeys.Ascending(MongoStreamHeadFields.Unsnapshotted), IndexOptions.SetName(MongoStreamIndexes.Unsnapshotted).SetUnique(false) ); if (_options.ServerSideOptimisticLoop) { PersistedCommits.Database.GetCollection("system.js").Save(new BsonDocument { { "_id", "insertCommit" }, { "value", new BsonJavaScript(_options.GetInsertCommitScript()) } }); } EmptyRecycleBin(); }); }
public void TestTextOptions() { var options = IndexOptions <TestClass> .SetName("custom").SetTextDefaultLanguage("spanish").SetTextLanguageOverride(x => x.idioma).SetWeight(x => x.textfield, 2); string expected = "{ \"name\" : \"custom\", \"default_language\" : \"spanish\", \"language_override\" : \"idioma\", \"weights\" : { \"textfield\" : 2 } }"; Assert.AreEqual(expected, options.ToJson()); }
private void Init() { IndexKeysBuilder keys = IndexKeys.Ascending("metadata.directory_name"); IndexOptionsBuilder options = IndexOptions.SetName("directory_name").SetBackground(false); MongoGridFS mongoGridFs = GetGridFS(); mongoGridFs.EnsureIndexes(); mongoGridFs.Files.EnsureIndex(keys, options); }
private static void FsFiles(MongoDatabase database) { var usersCollection = database.GetCollection("fs.files"); var userIndexes = usersCollection.GetIndexes(); if (userIndexes.All(c => c.Name != "SiteId")) { var keys = IndexKeys.Ascending("metadata.id", "metadata.type"); var options = IndexOptions.SetName("SiteId"); usersCollection.CreateIndex(keys, options); } }
private static void Users(MongoDatabase database) { var usersCollection = database.GetCollection("site.user"); var userIndexes = usersCollection.GetIndexes(); if (userIndexes.All(c => c.Name != "UserLogins")) { var keys = IndexKeys.Ascending("Logins.LoginProvider", "Logins.ProviderKey"); var options = IndexOptions.SetName("Logins"); usersCollection.CreateIndex(keys, options); } }
private void IndexExamples() { //insert IndexKeysBuilder <Car> carIndexBuilder = IndexKeys <Car> .Ascending(c => c.Make, c => c.NumberOfDoors); IndexOptionsBuilder <Car> carIndexOptions = IndexOptions <Car> .SetName("Car_CompositeIndex").SetTimeToLive(new TimeSpan(2, 0, 0, 0)); CarRentalContext.Cars.EnsureIndex(carIndexBuilder, carIndexOptions); //delete CarRentalContext.Cars.DropIndexByName("Car_CompositeIndex"); }
public void CreateIndex_with_wildcard_index_should_create_expected_index() { RequireServer.Check().Supports(Feature.WildcardIndexes); var collection = _database.GetCollection <Test>("test_wildcard_index"); collection.Drop(); collection.CreateIndex( IndexKeys <Test> .Wildcard(c => c.A), IndexOptions.SetName("custom")); var indexes = collection.GetIndexes(); var index = indexes.RawDocuments.Single(i => i["name"].AsString == "custom"); index["key"]["a.$**"].AsInt32.Should().Be(1); }
private void EnsureUniqueIndex(IContainSagaData saga, KeyValuePair <string, object> uniqueProperty) { Contract.Requires(saga != null); var collection = this.mongoDatabase.GetCollection(saga.GetType().Name); var indexOptions = IndexOptions.SetName(uniqueProperty.Key).SetUnique(true).SetSparse(true); var result = collection.CreateIndex(IndexKeys.Ascending(uniqueProperty.Key), indexOptions); if (result.HasLastErrorMessage) { throw new InvalidOperationException( string.Format( "Unable to create unique index on {0}: {1}", saga.GetType().Name, uniqueProperty.Key)); } }
private void EnsureTimeoutIndexes() { var collection = this.mongoDatabase.GetCollection <TimeoutData>(TimeoutDataName); var indexOptions = IndexOptions.SetName(MongoPersistenceConstants.OwningTimeoutManagerAndTimeName); var result = collection.CreateIndex( IndexKeys <TimeoutData> .Ascending(t => t.Time, t => t.OwningTimeoutManager), indexOptions); if (result.HasLastErrorMessage) { throw new InvalidOperationException( string.Format( "Unable to create {0} index", MongoPersistenceConstants.OwningTimeoutManagerAndTimeName)); } }
/// <summary> /// Initializes a new instance of the <see cref="Repository{TEntity, TId}"/> class. /// </summary> /// <param name="connectionName">Name of the connection.</param> /// <param name="collection">The collection.</param> /// <param name="indexes">The indexes.</param> public Repository(string connectionName, string collection, IEnumerable <MongoIndex> indexes) { _mongoHelper = MongoHelperProvider.Instance.GetHelper(connectionName); _collection = collection; if (indexes == null || !indexes.Any()) { return; } var mongoCollection = _mongoHelper.Repository.GetCollection <TEntity>(_collection); foreach (MongoIndex index in indexes) { mongoCollection.EnsureIndex(index.GetKey(), IndexOptions.SetName(index.Name)); } }
private MongoCollection <BsonDocument> GetAccountCollection() { MongoCollection <BsonDocument> collection; if (!_collectionDict.TryGetValue(_accountCollection, out collection)) { lock (this) { var client = new MongoClient(_connectionString); var database = client.GetServer().GetDatabase(new MongoUrl(_connectionString).DatabaseName); collection = database.GetCollection(_accountCollection); collection.EnsureIndex(IndexKeys.Ascending("Name"), IndexOptions.SetName("AccountNameUniqueIndex").SetUnique(true)); _collectionDict.TryAdd(_accountCollection, collection); } } return(collection); }
private void EnsureIndexes() { _commits.EnsureIndex( IndexKeys.Ascending("CommitId"), IndexOptions.SetName("CommitIdIndex")); _commits.EnsureIndex( IndexKeys.Ascending("Processed"), IndexOptions.SetName("ProcessedIndex")); _commits.EnsureIndex( IndexKeys.Descending("EventSourceId", "FromVersion"), IndexOptions.SetName("OptimisticEventSourceConcurrencyIndex").SetUnique(true)); _events.EnsureIndex( IndexKeys.Ascending("EventIdentifier"), IndexOptions.SetName("EventIdentifierIndex")); }
public void TestSetHintByIndexName() { collection.DropAllIndexes(); collection.RemoveAll(); collection.Insert(new BsonDocument { { "x", 1 }, { "y", 2 } }); collection.CreateIndex(IndexKeys.Ascending("x"), IndexOptions.SetName("xIndex")); var query = Query.EQ("x", 1); var cursor = collection.Find(query).SetHint("xIndex"); var count = 0; foreach (var document in cursor) { Assert.AreEqual(1, ++count); Assert.AreEqual(1, document["x"].AsInt32); } }
public void Handle(CurrenciesCreated message) { BsonDocument ccy = BsonDocument.Parse(message.CurrenciesCreatedJson); BsonDocument index = new BsonDocument(); index["_id"] = ccy["_id"]; index["Name"] = ccy["Name"]; index["Code"] = ccy["Code"]; index["OwnerId"] = ccy["OwnerId"]; index["Keywords"] = BsonValue.Create(new string[] { ccy["_id"].ToString(), ccy["Name"].ToString(), ccy["Code"].ToString(), ccy["OwnerId"].ToString() }); Collections.Save(index); Collections.EnsureIndex(IndexKeys.Descending("Keywords"), IndexOptions.SetName("Keywords")); }
public void CreateIndex_with_wildcardProjection_should_create_expected_index() { RequireServer.Check().Supports(Feature.WildcardIndexes); var collection = _database.GetCollection <Test>("test_wildcard_index"); collection.Drop(); collection.CreateIndex( IndexKeys <Test> .Wildcard(), IndexOptions <Test> .SetName("custom") .SetWildcardProjection(c => c.B, true) .SetWildcardProjection(c => c.Id, false)); var indexes = collection.GetIndexes(); var index = indexes.RawDocuments.Single(i => i["name"].AsString == "custom"); index["key"]["$**"].AsInt32.Should().Be(1); index["wildcardProjection"].ToBsonDocument().Should().Be(BsonDocument.Parse("{ b : 1, _id : 0 }")); }
private static void Items(MongoDatabase database) { var itemsCollection = database.GetCollection("site.item"); var itemIndexes = itemsCollection.GetIndexes(); if (itemIndexes.All(c => c.Name != "SiteId")) { var keys = IndexKeys.Ascending("SiteId"); var options = IndexOptions.SetName("SiteId"); itemsCollection.CreateIndex(keys, options); } if (itemIndexes.All(c => c.Name != "ParentId")) { var keys = IndexKeys.Ascending("SiteId", "ParentId"); var options = IndexOptions.SetName("ParentId"); itemsCollection.CreateIndex(keys, options); } }
public void Handle(TaxCreated message) { BsonDocument tax = BsonDocument.Parse(message.TaxJson); BsonDocument index = new BsonDocument(); index["_id"] = tax["_id"]; index["Name"] = tax["Name"]; index["Value"] = tax["Value"]; index["OwnerId"] = tax["OwnerId"]; index["Keywords"] = BsonValue.Create(new string[4] { tax["_id"].ToString(), tax["Name"].ToString(), tax["Value"].ToString(), tax["OwnerId"].ToString() }); Collections.Save(index); Collections.EnsureIndex(IndexKeys.Descending("Keywords"), IndexOptions.SetName("Keywords")); }
private static void Seo(MongoDatabase database) { var seoCollection = database.GetCollection("site.seo"); var seoIndexes = seoCollection.GetIndexes(); if (seoIndexes.All(c => c.Name != "Url")) { var keys = IndexKeys.Ascending("Url"); var options = IndexOptions.SetName("Url"); seoCollection.CreateIndex(keys, options); } if (seoIndexes.All(c => c.Name != "SiteId")) { var keys = IndexKeys.Ascending("SiteId"); var options = IndexOptions.SetName("SiteId"); seoCollection.CreateIndex(keys, options); } }
private static void Cache(MongoDatabase database) { var cacheCollection = database.GetCollection("site.cache"); var cacheIndexes = cacheCollection.GetIndexes(); if (cacheIndexes.All(c => c.Name != "SiteId")) { var keys = IndexKeys.Ascending("SiteId"); var options = IndexOptions.SetName("SiteId"); cacheCollection.CreateIndex(keys, options); } if (cacheIndexes.All(c => c.Name != "ParentId")) { var keys = IndexKeys.Ascending("Key", "Type"); var options = IndexOptions.SetName("KeyType"); cacheCollection.CreateIndex(keys, options); } }
private static void Stats(MongoDatabase database) { var statsCollection = database.GetCollection("site.stats"); var statIndexes = statsCollection.GetIndexes(); if (statIndexes.All(c => c.Name != "SiteId")) { var keys = IndexKeys.Ascending("SiteId"); var options = IndexOptions.SetName("SiteId"); statsCollection.CreateIndex(keys, options); } if (statIndexes.All(c => c.Name != "CreateDate")) { var keys = IndexKeys.Ascending("CreateDate"); var options = IndexOptions.SetName("CreateDate"); statsCollection.CreateIndex(keys, options); } }
private MongoCollection <BsonDocument> GetMongoCollection() { MongoCollection <BsonDocument> collection; if (_collectionDict.TryGetValue(UsernameCollectionName, out collection)) { return(collection); } lock (this) { var client = new MongoClient(ConnectionString); var db = client.GetServer().GetDatabase(new MongoUrl(ConnectionString).DatabaseName); collection = db.GetCollection(UsernameCollectionName); collection.EnsureIndex(IndexKeys.Ascending("UserName"), IndexOptions.SetName("UserNameUniqueIndex").SetUnique(true)); _collectionDict.TryAdd(UsernameCollectionName, collection); } return(collection); }