public void SuccessfullyRemoveRange() { var connection = TestConfiguration.GetConnection(); var context = new MongoDbContext(connection); var dbSet = new MongoDbSet <TestModel>(context); var entities = new[] { new TestModel { Description = "SuccessfullyRemoveRange.1" }, new TestModel { Description = "SuccessfullyRemoveRange.2" } }; dbSet.AddRange(entities); context.SaveChanges(); dbSet = new MongoDbSet <TestModel>(context); dbSet.RemoveRange(entities); Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); context.SaveChanges(); Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); }
public void SuccessfullyRemoveRangeByPredicate() { var connection = TestConfiguration.GetConnection(); var context = new MongoDbContext(connection); var dbSet = new MongoDbSet <TestModel>(context); var entities = new[] { new TestModel { Description = "SuccessfullyRemoveRangeByPredicate" }, new TestModel { Description = "SuccessfullyRemoveRangeByPredicate", BooleanField = true } }; dbSet.AddRange(entities); context.SaveChanges(); dbSet = new MongoDbSet <TestModel>(context); dbSet.RemoveRange(e => e.BooleanField); Assert.AreEqual(2, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); context.SaveChanges(); Assert.AreEqual(1, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); Assert.IsNotNull(dbSet.FirstOrDefault(m => m.Id == entities[0].Id)); }
public void SearchGeoNearWithCustomDistanceField() { var connection = TestConfiguration.GetConnection(); var dbSet = new MongoDbSet <SearchGeoModel>(); dbSet.SetConnection(connection); dbSet.AddRange(new SearchGeoModel[] { new SearchGeoModel { Description = "New York", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(-74.005974, 40.712776) ) }, new SearchGeoModel { Description = "Adelaide", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(138.600739, -34.928497) ) } }); dbSet.SaveChanges(); var results = dbSet.SearchGeoNear(e => e.PrimaryCoordinates, new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(138, -30) ), distanceResultField: e => e.CustomDistanceField).ToArray(); Assert.AreNotEqual(0, results[0].CustomDistanceField); Assert.AreNotEqual(0, results[1].CustomDistanceField); Assert.IsTrue(results[0].CustomDistanceField < results[1].CustomDistanceField); Assert.IsNull(results[0].ExtraElements); }
public void SuccessfullyAttachUntrackedEntities() { var connection = TestConfiguration.GetConnection(); var context = new MongoDbContext(connection); var dbSet = new MongoDbSet <DbSetModel>(context); var entities = new[] { new DbSetModel { Id = "abcd" }, new DbSetModel { Id = "efgh" } }; dbSet.AddRange(entities); context.SaveChanges(); ResetMongoDb(); context = new MongoDbContext(connection); dbSet = new MongoDbSet <DbSetModel>(context); var result = dbSet.AsNoTracking().ToList(); context.AttachRange(result); Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result[0]).State); Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result[1]).State); }
public void SearchGeoNearWithMinMaxDistances() { var connection = TestConfiguration.GetConnection(); var dbSet = new MongoDbSet <SearchGeoModel>(); dbSet.SetConnection(connection); dbSet.AddRange(new SearchGeoModel[] { new SearchGeoModel { Description = "New York", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(-74.005974, 40.712776) ) }, new SearchGeoModel { Description = "Adelaide", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(138.600739, -34.928497) ) }, new SearchGeoModel { Description = "Perth", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(115.860458, -31.950527) ) }, new SearchGeoModel { Description = "Hobart", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(147.327194, -42.882137) ) } }); dbSet.SaveChanges(); SearchGeoModel[] GetResults(double?maxDistance = null, double?minDistance = null) { return(dbSet.SearchGeoNear(e => e.PrimaryCoordinates, new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(138, -30) ), distanceResultField: e => e.CustomDistanceField, maxDistance: maxDistance, minDistance: minDistance).ToArray()); } var results = GetResults(maxDistance: 3000000); Assert.AreEqual(3, results.Count()); Assert.IsTrue(results.Max(e => e.CustomDistanceField) < 3000000); results = GetResults(maxDistance: 600000); Assert.AreEqual(1, results.Count()); Assert.IsTrue(results.Max(e => e.CustomDistanceField) < 600000); results = GetResults(maxDistance: 17000000); Assert.AreEqual(4, results.Count()); results = GetResults(minDistance: 600000); Assert.AreEqual(3, results.Count()); Assert.IsTrue(results.Min(e => e.CustomDistanceField) > 600000); results = GetResults(maxDistance: 3000000, minDistance: 600000); Assert.AreEqual(2, results.Count()); Assert.IsTrue(results.Max(e => e.CustomDistanceField) < 3000000); Assert.IsTrue(results.Min(e => e.CustomDistanceField) > 600000); }
public void SearchText() { var connection = TestConfiguration.GetConnection(); var context = new MongoDbContext(connection); var dbSet = new MongoDbSet <SearchTextModel>(context); dbSet.AddRange(new SearchTextModel[] { new SearchTextModel { MiscField = 1, Text = "The quick brown fox jumps over the lazy dog." }, new SearchTextModel { MiscField = 2, Text = "The five boxing wizards jump quickly." }, new SearchTextModel { MiscField = 3, Text = "The quick brown fox jumps over the lazy dog." }, new SearchTextModel { MiscField = 4, Text = "Jived fox nymph grabs quick waltz." }, }); context.SaveChanges(); Assert.AreEqual(4, dbSet.SearchText("quick").Count()); Assert.AreEqual(0, dbSet.SearchText("the").Count()); //Stop words aren't used in text indexes: https://docs.mongodb.com/manual/core/index-text/#supported-languages-and-stop-words Assert.AreEqual(2, dbSet.SearchText("dog").Count()); Assert.AreEqual(1, dbSet.SearchText("jived").Count()); Assert.AreEqual(1, dbSet.SearchText("quick").Where(e => e.MiscField == 3).Count()); }
public void WhereIdMatchesStringIds() { var connection = TestConfiguration.GetConnection(); var context = new MongoDbContext(connection); var dbSet = new MongoDbSet <WhereIdMatchesStringModel>(context); var entityCollection = new[] { new WhereIdMatchesStringModel { Description = "1" }, new WhereIdMatchesStringModel { Description = "2" }, new WhereIdMatchesStringModel { Description = "3" }, new WhereIdMatchesStringModel { Description = "4" } }; dbSet.AddRange(entityCollection); context.SaveChanges(); var provider = new MongoFrameworkQueryProvider <WhereIdMatchesStringModel>(connection); var queryable = new MongoFrameworkQueryable <WhereIdMatchesStringModel>(provider); var entityIds = entityCollection.Select(e => e.Id).Take(2); var idMatchQueryable = LinqExtensions.WhereIdMatches(queryable, entityIds); Assert.AreEqual(2, idMatchQueryable.Count()); Assert.IsTrue(idMatchQueryable.ToList().All(e => entityIds.Contains(e.Id))); }
public void SuccessfullyUpdateRange() { var dbSet = new MongoDbSet <MongoDbSetValidationModel>(); dbSet.SetConnection(TestConfiguration.GetConnection()); var entities = new[] { new MongoDbSetValidationModel { RequiredField = "SuccessfullyUpdateRange.1" }, new MongoDbSetValidationModel { RequiredField = "SuccessfullyUpdateRange.2" } }; dbSet.AddRange(entities); dbSet.SaveChanges(); dbSet.SetConnection(TestConfiguration.GetConnection()); entities[0].RequiredField = "SuccessfullyUpdateRange.1-Updated"; entities[1].RequiredField = "SuccessfullyUpdateRange.2-Updated"; dbSet.UpdateRange(entities); Assert.IsFalse(dbSet.Any(m => m.RequiredField == "SuccessfullyUpdateRange.1-Updated" || m.RequiredField == "SuccessfullyUpdateRange.2-Updated")); dbSet.SaveChanges(); Assert.IsTrue(dbSet.Any(m => m.RequiredField == "SuccessfullyUpdateRange.1-Updated")); Assert.IsTrue(dbSet.Any(m => m.RequiredField == "SuccessfullyUpdateRange.2-Updated")); }
public void SuccessfullyRemoveRangeByPredicate() { var dbSet = new MongoDbSet <MongoDbSetValidationModel>(); dbSet.SetConnection(TestConfiguration.GetConnection()); var entities = new[] { new MongoDbSetValidationModel { RequiredField = "SuccessfullyRemoveRangeByPredicate" }, new MongoDbSetValidationModel { RequiredField = "SuccessfullyRemoveRangeByPredicate", BooleanField = true } }; dbSet.AddRange(entities); dbSet.SaveChanges(); dbSet.SetConnection(TestConfiguration.GetConnection()); dbSet.RemoveRange(e => e.BooleanField); Assert.AreEqual(2, dbSet.Count(m => m.RequiredField == "SuccessfullyRemoveRangeByPredicate")); dbSet.SaveChanges(); Assert.AreEqual(1, dbSet.Count(m => m.RequiredField == "SuccessfullyRemoveRangeByPredicate")); Assert.IsNotNull(dbSet.FirstOrDefault(m => m.Id == entities[0].Id)); }
public void SearchGeoNearRecordLimits() { var connection = TestConfiguration.GetConnection(); var dbSet = new MongoDbSet <SearchGeoModel>(); dbSet.SetConnection(connection); for (var i = 0; i < 100; i++) { dbSet.Add(new SearchGeoModel { Description = $"Adelaide ({i})", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(138.600739, -34.928497) ) }); } dbSet.AddRange(new SearchGeoModel[] { new SearchGeoModel { Description = "New York", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(-74.005974, 40.712776) ) }, new SearchGeoModel { Description = "Perth", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(115.860458, -31.950527) ) }, new SearchGeoModel { Description = "Hobart", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(147.327194, -42.882137) ) } }); dbSet.SaveChanges(); IQueryable <SearchGeoModel> WithGeoQuery() { return(dbSet.SearchGeoNear(e => e.PrimaryCoordinates, new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(138, -30) ))); } Assert.AreEqual(103, WithGeoQuery().Count()); Assert.AreEqual(3, WithGeoQuery().Skip(100).Count()); var afterSkipResult = WithGeoQuery().Skip(100).FirstOrDefault(); Assert.AreEqual("Hobart", afterSkipResult.Description); var afterTakeResult = WithGeoQuery().Take(3).ToArray(); Assert.AreEqual(3, afterTakeResult.Length); Assert.AreEqual("Adelaide (0)", afterTakeResult[0].Description); }
#pragma warning disable CRR0026 // Unused member - used via Reflection private void CommitRelationship <TRelatedEntity>(EntityRelationship relationship, IEnumerable <TEntity> entities) where TRelatedEntity : class { var collection = BuildRelatedEntityCollection <TRelatedEntity>(relationship, entities); if (collection.Any()) { var dbSet = new MongoDbSet <TRelatedEntity>(); dbSet.SetConnection(Connection); dbSet.AddRange(collection); dbSet.SaveChanges(); } if (!relationship.IsCollection) { ApplyForeignKeyChanges <TRelatedEntity>(relationship, entities); } }
#pragma warning restore CRR0026 // Unused member - used via Reflection #pragma warning disable CRR0026 // Unused member - used via Reflection private async Task CommitRelationshipAsync <TRelatedEntity>(EntityRelationship relationship, IEnumerable <TEntity> entities, CancellationToken cancellationToken) where TRelatedEntity : class { var collection = BuildRelatedEntityCollection <TRelatedEntity>(relationship, entities); cancellationToken.ThrowIfCancellationRequested(); if (collection.Any()) { var dbSet = new MongoDbSet <TRelatedEntity>(); dbSet.SetConnection(Connection); dbSet.AddRange(collection); await dbSet.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } if (!relationship.IsCollection) { ApplyForeignKeyChanges <TRelatedEntity>(relationship, entities); } }
public void SearchGeoNear() { var connection = TestConfiguration.GetConnection(); var dbSet = new MongoDbSet <SearchGeoModel>(); dbSet.SetConnection(connection); dbSet.AddRange(new SearchGeoModel[] { new SearchGeoModel { Description = "New York", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(-74.005974, 40.712776) ) }, new SearchGeoModel { Description = "Adelaide", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(138.600739, -34.928497) ) }, new SearchGeoModel { Description = "Perth", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(115.860458, -31.950527) ) }, new SearchGeoModel { Description = "Hobart", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(147.327194, -42.882137) ) } }); dbSet.SaveChanges(); var results = dbSet.SearchGeoNear(e => e.PrimaryCoordinates, new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(138, -30) )).ToArray(); Assert.AreEqual(4, results.Count()); Assert.AreEqual("Adelaide", results[0].Description); Assert.AreEqual("New York", results[3].Description); Assert.IsTrue(results[0].ExtraElements.ContainsKey("Distance")); }
public void ReadAndWriteUnknownPropertyTypeEntity() { var connection = TestConfiguration.GetConnection(); var dbSet = new MongoDbSet <UnknownPropertyTypeModel>(); dbSet.SetConnection(connection); var entities = new[] { new UnknownPropertyTypeModel(), new UnknownPropertyTypeModel { UnknownItem = new UnknownPropertyTypeChildModel { Description = "UnknownPropertyTypeChildModel" } }, new UnknownPropertyTypeModel { UnknownItem = new Dictionary <string, int> { { "Age", 1 } } } }; dbSet.AddRange(entities); dbSet.SaveChanges(); ResetMongoDb(); dbSet = new MongoDbSet <UnknownPropertyTypeModel>(); dbSet.SetConnection(connection); var dbEntities = dbSet.ToArray(); Assert.IsNull(dbEntities[0].UnknownItem); Assert.IsInstanceOfType(dbEntities[1].UnknownItem, typeof(UnknownPropertyTypeChildModel)); Assert.IsInstanceOfType(dbEntities[2].UnknownItem, typeof(Dictionary <string, object>)); }
public void SearchGeoIntersects() { var connection = TestConfiguration.GetConnection(); var dbSet = new MongoDbSet <SearchGeoModel>(); dbSet.SetConnection(connection); dbSet.AddRange(new SearchGeoModel[] { new SearchGeoModel { Description = "New York", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(-74.005974, 40.712776) ) }, new SearchGeoModel { Description = "Adelaide", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(138.600739, -34.928497) ) }, new SearchGeoModel { Description = "Sydney", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(151.209290, -33.868820) ) }, new SearchGeoModel { Description = "Melbourne", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(144.963058, -37.813629) ) }, new SearchGeoModel { Description = "Darwin", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(-95.582413, 37.095142) ) }, new SearchGeoModel { Description = "Brisbane", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>( new GeoJson2DGeographicCoordinates(153.025131, -27.469770) ) } }); dbSet.SaveChanges(); var results = dbSet.SearchGeoIntersecting(e => e.PrimaryCoordinates, new GeoJsonPolygon <GeoJson2DGeographicCoordinates>( new GeoJsonPolygonCoordinates <GeoJson2DGeographicCoordinates>( new GeoJsonLinearRingCoordinates <GeoJson2DGeographicCoordinates>( new[] { new GeoJson2DGeographicCoordinates(115.860458, -31.950527), //Perth new GeoJson2DGeographicCoordinates(147.327194, -42.882137), //Hobart new GeoJson2DGeographicCoordinates(153.399994, -28.016666), //Gold Coast new GeoJson2DGeographicCoordinates(115.860458, -31.950527) //Wrap back to first point } ) ) )).ToArray(); Assert.AreEqual(3, results.Count()); Assert.IsTrue(results.Any(e => e.Description == "Adelaide")); Assert.IsTrue(results.Any(e => e.Description == "Melbourne")); Assert.IsTrue(results.Any(e => e.Description == "Sydney")); }