The settings used to access a collection.
 public void TestConstructorArgumentChecking()
 {
     var settings = new MongoCollectionSettings<BsonDocument>(_database, "");
     Assert.Throws<ArgumentNullException>(() => { new MongoCollection<BsonDocument>(null, settings); });
     Assert.Throws<ArgumentNullException>(() => { new MongoCollection<BsonDocument>(_database, null); });
     Assert.Throws<ArgumentOutOfRangeException>(() => { new MongoCollection<BsonDocument>(_database, settings); });
 }
        public void TestAll()
        {
            var settings = new MongoCollectionSettings();
            settings.CollectionName = "collection";
            settings.AssignIdOnInsert = false;
            settings.SafeMode = SafeMode.Create(5, TimeSpan.FromSeconds(5));
            settings.SlaveOk = true;

            Assert.AreEqual("collection", settings.CollectionName);
            Assert.AreEqual(false, settings.AssignIdOnInsert);
            Assert.AreEqual(SafeMode.Create(5, TimeSpan.FromSeconds(5)), settings.SafeMode);
            Assert.AreEqual(true, settings.SlaveOk);

            Assert.IsFalse(settings.IsFrozen);
            var hashCode = settings.GetHashCode();
            var stringRepresentation = settings.ToString();
            Assert.AreEqual(settings, settings);

            settings.Freeze();
            Assert.IsTrue(settings.IsFrozen);
            Assert.AreEqual(hashCode, settings.GetHashCode());
            Assert.AreEqual(stringRepresentation, settings.ToString());

            var clone = settings.Clone();
            Assert.IsFalse(clone.IsFrozen);
            Assert.AreEqual(settings, clone);
        }
		public static MongoCollectionSettings CollectionSettings()
		{
			var collectionSettings = new MongoCollectionSettings();
			collectionSettings.WriteConcern = WriteConcern.WMajority;

			return collectionSettings;
		}
        public void TestAll()
        {
            var settings = new MongoCollectionSettings<BsonDocument>(
                "collection",
                true, // asssignIdOnInsert
                SafeMode.Create(5, TimeSpan.FromSeconds(5)),
                true // slaveOkd
            );

            Assert.AreEqual("collection", settings.CollectionName);
            Assert.AreEqual(true, settings.AssignIdOnInsert);
            Assert.AreEqual(typeof(BsonDocument), settings.DefaultDocumentType);
            Assert.AreEqual(SafeMode.Create(5, TimeSpan.FromSeconds(5)), settings.SafeMode);
            Assert.AreEqual(true, settings.SlaveOk);

            Assert.IsFalse(settings.IsFrozen);
            var hashCode = settings.GetHashCode();
            var stringRepresentation = settings.ToString();
            Assert.AreEqual(settings, settings);

            settings.Freeze();
            Assert.IsTrue(settings.IsFrozen);
            Assert.AreEqual(hashCode, settings.GetHashCode());
            Assert.AreEqual(stringRepresentation, settings.ToString());
        }
Exemplo n.º 5
0
 public void TestFixtureSetup()
 {
     _database = Configuration.TestDatabase;
     var collectionSettings = new MongoCollectionSettings() { GuidRepresentation = GuidRepresentation.Standard };
     _collection = _database.GetCollection<C>("csharp714", collectionSettings);
     _collection.Drop();
 }
        public void TestEquals()
        {
            var settings = new MongoCollectionSettings();
            var clone = settings.Clone();
            Assert.IsTrue(clone.Equals(settings));

            settings.Freeze();
            clone.Freeze();
            Assert.IsTrue(clone.Equals(settings));

            clone = settings.Clone();
            clone.AssignIdOnInsert = !clone.AssignIdOnInsert;
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.GuidRepresentation = GuidRepresentation.PythonLegacy;
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.ReadPreference = ReadPreference.Secondary;
            Assert.IsFalse(clone.Equals(settings));

            clone = settings.Clone();
            clone.WriteConcern = WriteConcern.W2;
            Assert.IsFalse(clone.Equals(settings));
        }
Exemplo n.º 7
0
 public CSharp714Tests()
 {
     _database = LegacyTestConfiguration.Database;
     var collectionSettings = new MongoCollectionSettings() { GuidRepresentation = GuidRepresentation.Standard };
     _collection = _database.GetCollection<C>("csharp714", collectionSettings);
     _collection.Drop();
 }
        public void TestAll()
        {
            var server = MongoServer.Create();
            var database = server["test"];
            var settings = new MongoCollectionSettings<BsonDocument>(database, "collection")
            {
                AssignIdOnInsert = true,
                SafeMode = SafeMode.Create(5, TimeSpan.FromSeconds(5)),
                SlaveOk = true
            };

            Assert.AreEqual("collection", settings.CollectionName);
            Assert.AreEqual(true, settings.AssignIdOnInsert);
            Assert.AreEqual(typeof(BsonDocument), settings.DefaultDocumentType);
            Assert.AreEqual(GuidRepresentation.CSharpLegacy, settings.GuidRepresentation);
            Assert.AreEqual(SafeMode.Create(5, TimeSpan.FromSeconds(5)), settings.SafeMode);
            Assert.AreEqual(true, settings.SlaveOk);

            Assert.IsFalse(settings.IsFrozen);
            var hashCode = settings.GetHashCode();
            var stringRepresentation = settings.ToString();
            Assert.AreEqual(settings, settings);

            settings.Freeze();
            Assert.IsTrue(settings.IsFrozen);
            Assert.AreEqual(hashCode, settings.GetHashCode());
            Assert.AreEqual(stringRepresentation, settings.ToString());
        }
 public void TestConstructor()
 {
     var settings = new MongoCollectionSettings();
     Assert.AreEqual(false, settings.AssignIdOnInsert);
     Assert.AreEqual(GuidRepresentation.Unspecified, settings.GuidRepresentation);
     Assert.AreEqual(null, settings.ReadPreference);
     Assert.AreEqual(null, settings.WriteConcern);
 }
        public MongoEventStore(MongoClient client)
        {
            if (client == null) throw new ArgumentNullException("client");
            this.client = client;
            BsonClassMap.RegisterClassMap<MongoEventDocument>();
            _commitSettings = new MongoCollectionSettings {AssignIdOnInsert = false, WriteConcern = WriteConcern.Acknowledged};

        }
        /// <summary>
        /// Opens connection to MongoDB Server
        /// </summary>
        public MongoTransitionServer(String connectionString, String collectionName)
        {
            _collectionName = collectionName;
            _databaseName = MongoUrl.Create(connectionString).DatabaseName;
            _server = MongoServer.Create(connectionString);

            _transitionSettings = Database.CreateCollectionSettings<BsonDocument>(_collectionName);
            _transitionSettings.SafeMode = SafeMode.True;
            _transitionSettings.AssignIdOnInsert = false;
        }
Exemplo n.º 12
0
 internal static void Init(string connectionString)
 {
     var client = new MongoClient(connectionString);
     IMongoDatabase database = client.GetDatabase("sqlWithMongo");
     var collectionSettings = new MongoCollectionSettings
     {
         WriteConcern = new WriteConcern(1)
     };
     _collection = database.GetCollection<NodeLinks>("links", collectionSettings);
 }
Exemplo n.º 13
0
        public MongodbCollection(MongodbDatabase database, Type documentType, String collectionName)
        {
            if (collectionName == null) throw new ArgumentNullException("collectionName");

            _database = database;
            _documentType = documentType;
            _collectionName = collectionName;
            _metadata = _database.UniformDatabase.Metadata;

            var mongoSettings = new MongoCollectionSettings {AssignIdOnInsert = false};
            _collection = _database.Database.GetCollection(documentType, _collectionName, mongoSettings);
        }
 public void TestFrozenCopy()
 {
     var server = MongoServer.Create();
     var database = server["test"];
     var settings = new MongoCollectionSettings<BsonDocument>(database, "collection");
     var frozenCopy = settings.FrozenCopy();
     var secondFrozenCopy = frozenCopy.FrozenCopy();
     Assert.AreNotSame(settings, frozenCopy);
     Assert.AreSame(frozenCopy, secondFrozenCopy);
     Assert.AreEqual(false, settings.IsFrozen);
     Assert.AreEqual(true, frozenCopy.IsFrozen);
 }
        public void TestAssignIdOnInsert()
        {
            var settings = new MongoCollectionSettings<BsonDocument>(_database, "collection");
            Assert.AreEqual(MongoDefaults.AssignIdOnInsert, settings.AssignIdOnInsert);

            var assignIdOnInsert = !settings.AssignIdOnInsert;
            settings.AssignIdOnInsert = assignIdOnInsert;
            Assert.AreEqual(assignIdOnInsert, settings.AssignIdOnInsert);

            settings.Freeze();
            Assert.AreEqual(assignIdOnInsert, settings.AssignIdOnInsert);
            Assert.Throws<InvalidOperationException>(() => { settings.AssignIdOnInsert = assignIdOnInsert; });
        }
 public void TestClone()
 {
     // set everything to non default values to test that all settings are cloned
     var settings = new MongoCollectionSettings<BsonDocument>(_database, "collection")
     {
         AssignIdOnInsert = !MongoDefaults.AssignIdOnInsert,
         GuidRepresentation = GuidRepresentation.PythonLegacy,
         ReadPreference = ReadPreference.Secondary,
         WriteConcern = WriteConcern.W2
     };
     var clone = settings.Clone();
     Assert.IsTrue(clone.Equals(settings));
 }
        public void TestAssignIdOnInsert()
        {
            var settings = new MongoCollectionSettings();
            Assert.AreEqual(false, settings.AssignIdOnInsert);

            var assignIdOnInsert = !settings.AssignIdOnInsert;
            settings.AssignIdOnInsert = assignIdOnInsert;
            Assert.AreEqual(assignIdOnInsert, settings.AssignIdOnInsert);

            settings.Freeze();
            Assert.AreEqual(assignIdOnInsert, settings.AssignIdOnInsert);
            Assert.Throws<InvalidOperationException>(() => { settings.AssignIdOnInsert = assignIdOnInsert; });
        }
 public void Setup()
 {
     var settings = new MongoCollectionSettings();
     var dbSettings = new MongoDatabaseSettings();
     dbSettings.ApplyDefaultValues(new MongoServerSettings());
     settings.ApplyDefaultValues(dbSettings);
     _operationExecutor = new MockOperationExecutor();
     _subject = new MongoCollectionImpl<BsonDocument>(
         new CollectionNamespace("foo", "bar"),
         settings,
         Substitute.For<ICluster>(),
         _operationExecutor);
 }
        public void TestAll()
        {
            var settings = new MongoCollectionSettings
            {
                AssignIdOnInsert = true,
                GuidRepresentation = GuidRepresentation.PythonLegacy,
                ReadPreference = ReadPreference.Primary,
                WriteConcern = WriteConcern.Acknowledged
            };

            Assert.AreEqual(true, settings.AssignIdOnInsert);
            Assert.AreEqual(GuidRepresentation.PythonLegacy, settings.GuidRepresentation);
            Assert.AreSame(ReadPreference.Primary, settings.ReadPreference);
            Assert.AreSame(WriteConcern.Acknowledged, settings.WriteConcern);
        }
        public void TestBasic()
        {
            var server = MongoServer.Create();
            var database = server["test"];

            var it = new MongoCollectionSettings<Document>(database, "Document");

            var items = from item in database.GetCollection(it)
                        where item.ID == 1
                        select item;

            var count = Enumerable.Count(items);

            Assert.AreEqual(1, count);
        }
        public void TestAll()
        {
            var settings = new MongoCollectionSettings<BsonDocument>(_database, "collection")
            {
                AssignIdOnInsert = true,
                GuidRepresentation = GuidRepresentation.PythonLegacy,
                ReadPreference = ReadPreference.Primary,
                WriteConcern = WriteConcern.Acknowledged
            };

            Assert.AreEqual("collection", settings.CollectionName);
            Assert.AreEqual(typeof(BsonDocument), settings.DefaultDocumentType);
            Assert.AreEqual(true, settings.AssignIdOnInsert);
            Assert.AreEqual(GuidRepresentation.PythonLegacy, settings.GuidRepresentation);
            Assert.AreSame(ReadPreference.Primary, settings.ReadPreference);
            Assert.AreSame(WriteConcern.Acknowledged, settings.WriteConcern);
        }
Exemplo n.º 22
0
        public MongoResponseCache(string connectionString)
        {
            BsonSerializer.RegisterDiscriminatorConvention(typeof(Response), new TypeDiscriminatorConvention());

            BsonClassMap.RegisterClassMap<Response>(cm =>
            {
                cm.AutoMap();
                cm.MapProperty(x => x.Type).SetRepresentation(BsonType.String);
            });

            BsonClassMap.RegisterClassMap<Error>();
            BsonClassMap.RegisterClassMap<Link>();
            BsonClassMap.RegisterClassMap<Photo>();
            BsonClassMap.RegisterClassMap<Rich>();
            BsonClassMap.RegisterClassMap<Video>();

            var database = MongoDatabase.Create(connectionString);
            var settings = new MongoCollectionSettings<MongoCacheItem>(database, "embedly");
            _collection = database.GetCollection(settings);
        }
        public void TestCount()
        {
            var server = MongoServer.Create();
            var database = server["test"];

            var it = new MongoCollectionSettings<Document>(database, "Document");

            var col = database.GetCollection(it);

            var query = (from k in col where k.Name == "Vlad" select k);

            Assert.AreEqual(1, query.Count());
            Assert.AreEqual(typeof(int), query.Count().GetType());

            Assert.AreEqual(1, query.LongCount());
            Assert.AreEqual(typeof(long), query.LongCount().GetType());

            Assert.AreEqual(1, query.LongCount(k => k.ID == 1));
            Assert.AreEqual(typeof(long), query.LongCount(k => k.ID == 1).GetType());
        }
        public void TestAdvancedWhere()
        {
            var server = MongoServer.Create();
            var database = server["test"];

            var it = new MongoCollectionSettings<Document>(database, "Document");

            var rnd = new Random();
            var items = from item in database.GetCollection(it)
                        where item.ID == rnd.Next(0, 4)
                        select item;

            var count = 0;
            foreach (var item in items)
            {
                count++;
            }

            Assert.AreEqual(1, count);
        }
Exemplo n.º 25
0
        public void Flush()
        {
            long tobson = 0;

            int index = 0;
            Task[] tasks = new Task[_inMemoryDatabase.Collections.Keys.Count];

            foreach (var pair in _inMemoryDatabase.Collections)
            {
                var mongoSettings = new MongoCollectionSettings {AssignIdOnInsert = false};

                var mongoCollection = Database.GetCollection(typeof(BsonDocument), pair.Key.CollectionName, mongoSettings);
                var inMemoryCollection = (IInMemoryCollection) pair.Value;

                var stopwatch = Stopwatch.StartNew();
                var docs = BsonDocumentWrapper.CreateMultiple(inMemoryCollection.Documents.Values);
                stopwatch.Stop();
                //Console.WriteLine("Collection {0} serialized to bson in {1:n0} ms", pair.Key, stopwatch.ElapsedMilliseconds);
                tobson += stopwatch.ElapsedMilliseconds;

                stopwatch.Start();

                tasks[index] = Task.Factory.StartNew(() =>
                {
                    var mongoInsertOptions = new MongoInsertOptions();
                    mongoInsertOptions.CheckElementNames = false;
                    mongoInsertOptions.WriteConcern = WriteConcern.Acknowledged;
                    mongoCollection.InsertBatch(docs);
                }, TaskCreationOptions.LongRunning);

                stopwatch.Stop();
                //Console.WriteLine("Collection {0} inserted to MongoDB in {1:n0} ms", pair.Key, stopwatch.ElapsedMilliseconds);

                index++;
            }

            Task.WaitAll(tasks);

            //Console.WriteLine("Total time for serialization: {0:n0} ms", tobson);
        }
        public void TestDefaults()
        {
            var settings = new MongoCollectionSettings();

            Assert.AreEqual(null, settings.CollectionName);
            Assert.AreEqual(true, settings.AssignIdOnInsert);
            Assert.AreEqual(SafeMode.False, settings.SafeMode);
            Assert.AreEqual(false, settings.SlaveOk);

            Assert.IsFalse(settings.IsFrozen);
            var hashCode = settings.GetHashCode();
            var stringRepresentation = settings.ToString();
            Assert.AreEqual(settings, settings);

            settings.Freeze();
            Assert.IsTrue(settings.IsFrozen);
            Assert.AreEqual(hashCode, settings.GetHashCode());
            Assert.AreEqual(stringRepresentation, settings.ToString());

            var clone = settings.Clone();
            Assert.IsFalse(clone.IsFrozen);
            Assert.AreEqual(settings, clone);
        }
Exemplo n.º 27
0
        public Tailer(IMongoClient client, MongoCollectionSettings oplogCollectionSettings = null, string oplogCollectionName = "oplog.rs")
        {
            if(client == null)
            {
                throw new ArgumentNullException("client");
            }

            if(string.IsNullOrWhiteSpace(client.Settings.ReplicaSetName))
            {
                throw new MongoRiverException("Mongo client is not configured as a replica set");
            }

            if(oplogCollectionSettings == null)
            {
                oplogCollectionSettings = new MongoCollectionSettings
                {
                    // You probably don't want to be doing this on your primary
                    ReadPreference = ReadPreference.Secondary
                };
            }

            m_oplogCollection = client.GetDatabase("local").GetCollection<Oplog>(oplogCollectionName, oplogCollectionSettings);
        }
Exemplo n.º 28
0
        public override IMongoCollection <TDocument> GetCollection <TDocument>(string name, MongoCollectionSettings settings)
        {
            Ensure.IsNotNullOrEmpty(name, nameof(name));

            settings = settings == null ?
                       new MongoCollectionSettings() :
                       settings.Clone();

            settings.ApplyDefaultValues(_settings);

            return(new MongoCollectionImpl <TDocument>(this, new CollectionNamespace(_databaseNamespace, name), settings, _cluster, _operationExecutor));
        }
 /// <inheritdoc />
 public abstract IMongoCollection <TDocument> GetCollection <TDocument>(string name, MongoCollectionSettings settings = null);
Exemplo n.º 30
0
 /// <summary>
 /// Gets a MongoCollection instance representing a collection on this database
 /// with a default document type of TDefaultDocument.
 /// </summary>
 /// <typeparam name="TDefaultDocument">The default document type for this collection.</typeparam>
 /// <param name="collectionSettings">The settings to use when accessing this collection.</param>
 /// <returns>An instance of MongoCollection.</returns>
 public virtual MongoCollection <TDefaultDocument> GetCollection <TDefaultDocument>(MongoCollectionSettings <TDefaultDocument> collectionSettings)
 {
     lock (databaseLock)
     {
         MongoCollection collection;
         if (!collections.TryGetValue(collectionSettings, out collection))
         {
             collection = new MongoCollection <TDefaultDocument>(this, collectionSettings);
             collections.Add(collectionSettings, collection);
         }
         return((MongoCollection <TDefaultDocument>)collection);
     }
 }
Exemplo n.º 31
0
        private MongoCollectionImpl(IMongoDatabase database, CollectionNamespace collectionNamespace, MongoCollectionSettings settings, ICluster cluster, IOperationExecutor operationExecutor, IBsonSerializer <TDocument> documentSerializer)
        {
            _database            = Ensure.IsNotNull(database, nameof(database));
            _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
            _settings            = Ensure.IsNotNull(settings, nameof(settings)).Freeze();
            _cluster             = Ensure.IsNotNull(cluster, nameof(cluster));
            _operationExecutor   = Ensure.IsNotNull(operationExecutor, nameof(operationExecutor));
            _documentSerializer  = Ensure.IsNotNull(documentSerializer, nameof(documentSerializer));

            _messageEncoderSettings = new MessageEncoderSettings
            {
                { MessageEncoderSettingsName.GuidRepresentation, _settings.GuidRepresentation },
                { MessageEncoderSettingsName.ReadEncoding, _settings.ReadEncoding ?? Utf8Encodings.Strict },
                { MessageEncoderSettingsName.WriteEncoding, _settings.WriteEncoding ?? Utf8Encodings.Strict }
            };
        }
        // constructors
        public MongoCollectionImpl(IMongoDatabase database, CollectionNamespace collectionNamespace, MongoCollectionSettings settings, ICluster cluster, IOperationExecutor operationExecutor)
        {
            _database            = Ensure.IsNotNull(database, "database");
            _collectionNamespace = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
            _settings            = Ensure.IsNotNull(settings, "settings").Freeze();
            _cluster             = Ensure.IsNotNull(cluster, "cluster");
            _operationExecutor   = Ensure.IsNotNull(operationExecutor, "operationExecutor");

            _documentSerializer     = _settings.SerializerRegistry.GetSerializer <TDocument>();
            _messageEncoderSettings = new MessageEncoderSettings
            {
                { MessageEncoderSettingsName.GuidRepresentation, _settings.GuidRepresentation },
                { MessageEncoderSettingsName.ReadEncoding, _settings.ReadEncoding ?? Utf8Encodings.Strict },
                { MessageEncoderSettingsName.WriteEncoding, _settings.WriteEncoding ?? Utf8Encodings.Strict }
            };
        }
Exemplo n.º 33
0
 public void Dispose()
 {
     _collectionSettings = null;
     _client             = null;
     _db = null;
 }
Exemplo n.º 34
0
        /// <summary>
        /// Gets a MongoCollection instance representing a collection on this database
        /// with a default document type of BsonDocument.
        /// </summary>
        /// <param name="defaultDocumentType">The default document type.</param>
        /// <param name="collectionName">The name of the collection.</param>
        /// <returns>An instance of MongoCollection.</returns>
        public virtual MongoCollection GetCollection(Type defaultDocumentType, string collectionName)
        {
            var collectionSettings = new MongoCollectionSettings();

            return(GetCollection(defaultDocumentType, collectionName, collectionSettings));
        }
Exemplo n.º 35
0
        /// <summary>
        /// Gets a MongoCollection instance representing a collection on this database
        /// with a default document type of BsonDocument.
        /// </summary>
        /// <param name="defaultDocumentType">The default document type.</param>
        /// <param name="collectionName">The name of the collection.</param>
        /// <param name="collectionSettings">The settings to use when accessing this collection.</param>
        /// <returns>An instance of MongoCollection.</returns>
        public virtual MongoCollection GetCollection(Type defaultDocumentType, string collectionName, MongoCollectionSettings collectionSettings)
        {
            var collectionDefinition = typeof(MongoCollection <>);
            var collectionType       = collectionDefinition.MakeGenericType(defaultDocumentType);
            var constructorInfo      = collectionType.GetConstructor(new Type[] { typeof(MongoDatabase), typeof(string), typeof(MongoCollectionSettings) });

            return((MongoCollection)constructorInfo.Invoke(new object[] { this, collectionName, collectionSettings }));
        }
Exemplo n.º 36
0
 public virtual MongoCollection GetCollection(MongoCollectionSettings collectionSettings)
 {
     return(GetCollection(collectionSettings.DefaultDocumentType, collectionSettings.CollectionName, collectionSettings));
 }
Exemplo n.º 37
0
 /// <summary>
 /// Gets a MongoCollection instance representing a collection on this database
 /// with a default document type of TDefaultDocument.
 /// </summary>
 /// <param name="collectionName">The name of the collection.</param>
 /// <param name="collectionSettings">The settings to use when accessing this collection.</param>
 /// <returns>An instance of MongoCollection.</returns>
 public virtual MongoCollection <BsonDocument> GetCollection(string collectionName, MongoCollectionSettings collectionSettings)
 {
     return(GetCollection <BsonDocument>(collectionName, collectionSettings));
 }
Exemplo n.º 38
0
 /// <summary>
 /// Gets a MongoCollection instance representing a collection on this database
 /// with a default document type of TDefaultDocument.
 /// </summary>
 /// <typeparam name="TDefaultDocument">The default document type for this collection.</typeparam>
 /// <param name="collectionName">The name of the collection.</param>
 /// <param name="collectionSettings">The settings to use when accessing this collection.</param>
 /// <returns>An instance of MongoCollection.</returns>
 public virtual MongoCollection <TDefaultDocument> GetCollection <TDefaultDocument>(
     string collectionName, MongoCollectionSettings collectionSettings)
 {
     return(new MongoCollection <TDefaultDocument>(this, collectionName, collectionSettings));
 }
Exemplo n.º 39
0
 // constructors
 public MongoCollectionImpl(IMongoDatabase database, CollectionNamespace collectionNamespace, MongoCollectionSettings settings, ICluster cluster, IOperationExecutor operationExecutor)
     : this(database, collectionNamespace, settings, cluster, operationExecutor, Ensure.IsNotNull(settings, "settings").SerializerRegistry.GetSerializer <TDocument>())
 {
 }
 // public methods
 /// <summary>
 /// Creates a clone of the settings.
 /// </summary>
 /// <returns>A clone of the settings.</returns>
 public virtual MongoCollectionSettings Clone()
 {
     var clone = new MongoCollectionSettings();
     clone._assignIdOnInsert = _assignIdOnInsert.Clone();
     clone._guidRepresentation = _guidRepresentation.Clone();
     clone._readEncoding = _readEncoding.Clone();
     clone._readPreference = _readPreference.Clone();
     clone._writeConcern = _writeConcern.Clone();
     clone._writeEncoding = _writeEncoding.Clone();
     return clone;
 }
Exemplo n.º 41
0
        /// <summary>
        /// Gets a MongoCollection instance representing a collection on this database
        /// with a default document type of BsonDocument.
        /// </summary>
        /// <param name="collectionName">The name of the collection.</param>
        /// <returns>An instance of MongoCollection.</returns>
        public virtual MongoCollection <BsonDocument> GetCollection(string collectionName)
        {
            var collectionSettings = new MongoCollectionSettings <BsonDocument>(this, collectionName);

            return(GetCollection(collectionSettings));
        }
Exemplo n.º 42
0
        /// <summary>
        /// Gets a MongoCollection instance representing a collection on this database
        /// with a default document type of BsonDocument.
        /// </summary>
        /// <param name="defaultDocumentType">The default document type.</param>
        /// <param name="collectionName">The name of the collection.</param>
        /// <param name="collectionSettings">The settings to use when accessing this collection.</param>
        /// <returns>An instance of MongoCollection.</returns>
        public virtual MongoCollection GetCollection(Type defaultDocumentType, string collectionName, MongoCollectionSettings collectionSettings)
        {
            var collectionDefinition      = typeof(MongoCollection <>);
            var collectionType            = collectionDefinition.MakeGenericType(defaultDocumentType);
            var constructorParameterTypes = new Type[] { typeof(MongoDatabase), typeof(string), typeof(MongoCollectionSettings), typeof(IOperationExecutor) };
            var constructorInfo           = collectionType.GetTypeInfo()
                                            .GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)
                                            .Where(c => c.GetParameters().Select(p => p.ParameterType).SequenceEqual(constructorParameterTypes))
                                            .Single();

            return((MongoCollection)constructorInfo.Invoke(new object[] { this, collectionName, collectionSettings, _operationExecutor }));
        }
        public void TestStrictWrite()
        {
            var settings = new MongoCollectionSettings { WriteEncoding = new UTF8Encoding(false, true) };
            var collection = _database.GetCollection(Configuration.TestCollection.Name, settings);

            var document = new BsonDocument("x", "\udc00"); // invalid lone low surrogate
            Assert.Throws<EncoderFallbackException>(() => { collection.Insert(document); });
        }
Exemplo n.º 44
0
 public virtual MongoCollection <TDefaultDocument> GetCollection <TDefaultDocument>(
     MongoCollectionSettings <TDefaultDocument> collectionSettings)
 {
     return(GetCollection <TDefaultDocument>(collectionSettings.CollectionName, collectionSettings));
 }
        public void TestSaveD()
        {
            var collectionSettings = new MongoCollectionSettings<D>(_database, "test")
            {
                GuidRepresentation = GuidRepresentation.Standard
            };
            var collection = _database.GetCollection(collectionSettings);
            collection.Drop();

            var id = new Guid("00112233-4455-6677-8899-aabbccddeeff");
            var doc = new D { Id = id, X = 1 };
            collection.Insert(doc);

            Assert.AreEqual(1, collection.Count());
            var fetched = collection.FindOne();
            Assert.AreEqual(id, fetched.Id);
            Assert.AreEqual(1, fetched.X);

            doc.X = 2;
            collection.Save(doc);

            Assert.AreEqual(1, collection.Count());
            fetched = collection.FindOne();
            Assert.AreEqual(id, fetched.Id);
            Assert.AreEqual(2, fetched.X);
        }
Exemplo n.º 46
0
        /// <summary>
        /// Gets a MongoCollection instance representing a collection on this database
        /// with a default document type of TDefaultDocument.
        /// </summary>
        /// <typeparam name="TDefaultDocument">The default document type for this collection.</typeparam>
        /// <param name="collectionName">The name of the collection.</param>
        /// <returns>An instance of MongoCollection.</returns>
        public virtual MongoCollection <TDefaultDocument> GetCollection <TDefaultDocument>(string collectionName)
        {
            var collectionSettings = new MongoCollectionSettings();

            return(GetCollection <TDefaultDocument>(collectionName, collectionSettings));
        }