public void GetVersion_FromDb_Success() { // ARRANGE var db = ConnectionUtils.CreateDbContext(); // ACT var version = MongoVersionHelper.GetVersion(db.Database); // ASSERT // no exception }
public void GetVersion_FromDb_Success() { // ARRANGE using (var connection = ConnectionUtils.CreateDbContext()) { // ACT var version = MongoVersionHelper.GetVersion(connection.Database); } // ASSERT // no exception }
public void GetVersion_HasAdditionalInfo_Success() { // ARRANGE var dbMock = new Mock <IMongoDatabase>(MockBehavior.Strict); var command = new JsonCommand <BsonDocument>("{'buildinfo': 1}"); dbMock.Setup(m => m.RunCommand(It.Is <JsonCommand <BsonDocument> >(b => b.Json.Contains("buildinfo")), null, CancellationToken.None)) .Returns(new BsonDocument { ["version"] = "3.6.4-1.2" }); // ACT var version = MongoVersionHelper.GetVersion(dbMock.Object); // ASSERT Assert.Equal(version, new Version(3, 6, 4)); }
/// <summary> /// Backups the collection in database identified by collectionName. /// </summary> /// <param name="database">Referance to the mongo database.</param> /// <param name="collectionName">The name of the collection to backup.</param> /// <param name="backupCollectionName">Tha name of the backup collection.</param> protected virtual void BackupCollection(IMongoDatabase database, string collectionName, string backupCollectionName) { var aggregate = new BsonDocument(new Dictionary <string, object> { { "aggregate", collectionName }, { "pipeline", new [] { new Dictionary <string, object> { { "$match", new BsonDocument() } }, new Dictionary <string, object> { { "$out", backupCollectionName } } } }, { "allowDiskUse", true }, { // As of MongoDB 3.4 cursor is no longer // optional, but can be set to "empty". // https://docs.mongodb.com/manual/reference/command/aggregate/ "cursor", new BsonDocument() } }); var version = MongoVersionHelper.GetVersion(database); if (version < new Version(2, 6)) { throw new InvalidOperationException("Hangfire.Mongo is not able to backup collections in MongoDB running a version prior to 2.6"); } if (version >= new Version(3, 2)) { // The 'bypassDocumentValidation' was introduced in version 3.2 // https://docs.mongodb.com/manual/release-notes/3.2/#rel-notes-document-validation aggregate["bypassDocumentValidation"] = true; } var dbSource = database.GetCollection <BsonDocument>(collectionName); var indexes = dbSource.Indexes.List().ToList().Where(idx => idx["name"] != "_id_").ToList(); if (indexes.Any()) { var dbBackup = database.GetCollection <BsonDocument>(backupCollectionName); foreach (var index in indexes) { var newIndex = new BsonDocumentIndexKeysDefinition <BsonDocument>(index["key"].AsBsonDocument); var newIndexKeys = index.Names.ToList(); var newOptions = new CreateIndexOptions(); foreach (var key in newIndexKeys) { switch (key) { case "v": newOptions.Version = index[key].AsInt32; break; case "name": newOptions.Name = index[key].AsString; break; case "unique": newOptions.Unique = index[key].AsBoolean; break; case "sparse": newOptions.Sparse = index[key].AsBoolean; break; case "expireAfterSeconds": newOptions.ExpireAfter = TimeSpan.FromSeconds(index[key].AsInt64); break; case "background": newOptions.Background = index[key].AsBoolean; break; case "textIndexVersion": newOptions.TextIndexVersion = index[key].AsInt32; break; case "default_language": newOptions.DefaultLanguage = index[key].AsString; break; case "language_override": newOptions.LanguageOverride = index[key].AsString; break; case "weights": newOptions.Weights = index[key].AsBsonDocument; break; case "min": newOptions.Min = index[key].AsDouble; break; case "max": newOptions.Max = index[key].AsDouble; break; case "bits": newOptions.Bits = index[key].AsInt32; break; case "2dsphereIndexVersion": newOptions.SphereIndexVersion = index[key].AsInt32; break; case "bucketSize": newOptions.BucketSize = index[key].AsDouble; break; } } dbBackup.Indexes.CreateOne(newIndex, newOptions); } } database.RunCommand(new BsonDocumentCommand <BsonDocument>(aggregate)); }