Beispiel #1
0
 public void MapReduceCreatesACollection()
 {
     using (var mongo = Mongo.Create(TestHelper.ConnectionString("pooling=false")))
     {
         mongo.GetCollection <ReduceProduct>().Insert(new ReduceProduct {
             Price = 1.5f
         }, new ReduceProduct {
             Price = 2.5f
         });
         var mr     = mongo.Database.CreateMapReduce();
         var result = mr.Execute(new MapReduceOptions <ReduceProduct> {
             Map = _map, Reduce = _reduce
         });
         var found = false;
         foreach (var c in mongo.Database.GetAllCollections())
         {
             if (c.Name.EndsWith(result.Result))
             {
                 found = true;
                 break;
             }
         }
         Assert.Equal(true, found);
     }
 }
 public void Add <T>(IEnumerable <T> items) where T : class, new()
 {
     using (var db = Mongo.Create("_conn"))
     {
         db.GetCollection <T>().Insert(items);
     }
 }
 public void UpdateOne <T>(object item, object update)
 {
     using (var db = Mongo.Create("_conn"))
     {
         db.GetCollection <T>().UpdateOne(item, item);
     }
 }
 public void SaveItem <TItem>(TItem item)
 {
     using (var db = Mongo.Create("_conn"))
     {
         db.GetCollection <TItem>().Save(item);
     }
 }
 public TItem FindOne <TItem>(Func <TItem, bool> func)
 {
     using (var db = Mongo.Create("_conn"))
     {
         return(db.GetCollection <TItem>().AsQueryable().Where(func).FirstOrDefault());
     }
 }
Beispiel #6
0
        public void DbRefMapsToOtherDocumentsByCustomId()
        {
            const string userId   = "Tim Berners-Lee";
            const string roleName = "Administrator";

            using (var db = Mongo.Create(TestHelper.ConnectionString()))
            {
                db.GetCollection <User3>().Insert(new User3
                {
                    Id           = userId,
                    EmailAddress = "*****@*****.**"
                });
                db.GetCollection <Role>().Insert(new Role
                {
                    Id    = roleName,
                    Users = new List <DbReference <User3, string> >
                    {
                        new DbReference <User3, string>(userId)
                    }
                });

                var role = db.GetCollection <Role>().Find().First();
                var user = role.Users[0].Fetch(() => db);

                Assert.Equal(userId, user.Id);
            }
        }
Beispiel #7
0
 public void Delete <T>(object template) where T : class, new()
 {
     using (var db = Mongo.Create("_conn"))
     {
         db.GetCollection <T>().Delete(template);
     }
 }
Beispiel #8
0
 public void GetCollectionsReturnsNothingIfEmpty()
 {
     using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
     {
         Assert.Equal(0, mongo.Database.GetAllCollections().Count());
     }
 }
Beispiel #9
0
 public T Obter <T>(Func <T, bool> expressao) where T : class
 {
     using (var mongo = Mongo.Create(StringDeConexao))
     {
         return(mongo.GetCollection <T>().AsQueryable().Where(expressao).SingleOrDefault());
     }
 }
Beispiel #10
0
 public void ReturnsTheDatabasesName()
 {
     using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
     {
         Assert.Equal("NormTests", mongo.Database.DatabaseName);
     }
 }
Beispiel #11
0
 public void GettingStatisticsFailsSilentlyWithStrictModeOff()
 {
     using (var mongo = Mongo.Create(TestHelper.ConnectionString("&strict=false")))
     {
         Assert.Equal(null, mongo.Database.GetCollectionStatistics("temp"));
     }
 }
Beispiel #12
0
 public void DropCollectionFailsSilentlyWithStrictModeOff()
 {
     using (var mongo = Mongo.Create(TestHelper.ConnectionString("&strict=false")))
     {
         Assert.Equal(false, mongo.Database.DropCollection("temp"));
     }
 }
 public void AddHighScore(Score score)
 {
     using (IMongo mongo = Mongo.Create("mongodb://localhost/worms"))
     {
         mongo.GetCollection <Score>("Score").Insert(score);
     }
 }
Beispiel #14
0
        public void TemporaryCollectionIsCleanedUpWhenConnectionIsClosed()
        {
            string name;

            using (var mongo = Mongo.Create(TestHelper.ConnectionString("pooling=false")))
            {
                mongo.GetCollection <ReduceProduct>().Insert(new ReduceProduct {
                    Price = 1.5f
                }, new ReduceProduct {
                    Price = 2.5f
                });
                var mr = mongo.Database.CreateMapReduce();
                name = mr.Execute(new MapReduceOptions <ReduceProduct> {
                    Map = _map, Reduce = _reduce
                }).Result;
            }

            using (var mongo = Mongo.Create(TestHelper.ConnectionString("pooling=false")))
            {
                foreach (var c in mongo.Database.GetAllCollections())
                {
                    Assert.Equal(false, c.Name.EndsWith(name));
                }
            }
        }
Beispiel #15
0
        public void Find_Subset_Returns_Appropriate_Subset()
        {
            using (var admin = new MongoAdmin(TestHelper.ConnectionString()))
            {
                admin.SetProfileLevel(2);
            }
            using (var db = Mongo.Create(TestHelper.ConnectionString()))
            {
                var coll = db.GetCollection <TestProduct>();
                coll.Delete(new { });
                var oid = ObjectId.NewObjectId();

                coll.Insert(new TestProduct
                {
                    _id      = oid,
                    Price    = 42.42f,
                    Supplier = new Supplier
                    {
                        Name      = "Bob's house of pancakes",
                        RefNum    = 12,
                        CreatedOn = DateTime.MinValue
                    }
                });


                var subset = db.GetCollection <TestProduct>().Find(new { }, new { }, Int32.MaxValue, 0,
                                                                   j => new { SupplierName = j.Supplier.Name, Cost = j.Price, Id = j._id }).ToArray();

                Assert.Equal("Bob's house of pancakes", subset[0].SupplierName);
                Assert.Equal(42.42f, subset[0].Cost);
                Assert.Equal(oid, subset[0].Id);
            }
        }
Beispiel #16
0
 public List <T> ObterTodos <T>() where T : class
 {
     using (var mongo = Mongo.Create(StringDeConexao))
     {
         return(mongo.GetCollection <T>().AsQueryable().ToList());
     }
 }
        public void File_Save_Is_Not_Lossy()
        {
            using (var conn = Mongo.Create(TestHelper.ConnectionString()))
            {
                var ms = new MemoryStream(8000000);//about 8MB.
                for (int i = 0; i < 2000000; i++)
                {
                    ms.Write(BitConverter.GetBytes(i), 0, 4);
                }

                var gridFS = conn.Database.Files();
                var file   = new GridFile();
                file.ContentType = "application/unknown";
                file.FileName    = "Random_File_Test" + Guid.NewGuid().ToString();
                file.Aliases     = new String[] { "Alpha", "Bravo", "Delta", "Echo" };
                file.Content     = ms.ToArray();
                gridFS.Save(file);

                var file2 = gridFS.FindOne(new { _id = file.Id });


                Assert.Equal(file.Id, file2.Id);
                Assert.Equal(file.MD5Checksum, file2.MD5Checksum);
                Assert.Equal(file.ContentType, file2.ContentType);
                //Mongo stores dates as long, therefore, we have to use double->long rounding.
                Assert.Equal((long)((file.UploadDate - DateTime.MinValue)).TotalMilliseconds,
                             (long)(file2.UploadDate - DateTime.MinValue).TotalMilliseconds);
                Assert.True(file.Aliases.SequenceEqual(file2.Aliases));
                Assert.True(file.Content.SequenceEqual(file2.Content));
            }
        }
Beispiel #18
0
 public void Excluir <T>(T objeto) where T : class
 {
     using (var mongo = Mongo.Create(StringDeConexao))
     {
         mongo.GetCollection <T>().Delete(objeto);
     }
 }
 public void AddHighScore(Score score)
 {
     using (IMongo mongo = Mongo.Create(this.MongoURL))
     {
         mongo.GetCollection <Score>("Score").Insert(score);
     }
 }
Beispiel #20
0
 public void Salvar <T>(T objeto) where T : class
 {
     using (var mongo = Mongo.Create(StringDeConexao))
     {
         mongo.GetCollection <T>().Save(objeto);
     }
 }
Beispiel #21
0
 public void Delete <T>(T item) where T : class, new()
 {
     using (var db = Mongo.Create("_conn"))
     {
         db.GetCollection <T>().Delete(item);
     }
 }
Beispiel #22
0
 public void Delete <T>(T item) where T : class, new()
 {
     using (var db = Mongo.Create(_connectionString))
     {
         db.Database.GetCollection <T>().Delete(item);
     }
 }
 public TItem FindOne <TItem>(object spec)
 {
     using (var db = Mongo.Create("_conn"))
     {
         return(db.GetCollection <TItem>().FindOne(spec));
     }
 }
Beispiel #24
0
 public void DeleteAll <T>() where T : class, new()
 {
     using (var db = Mongo.Create(_connectionString))
     {
         db.Database.DropCollection(typeof(T).Name);
     }
 }
 public List <TItem> Find <TItem>(Func <TItem, bool> func)
 {
     using (var db = Mongo.Create("_conn"))
     {
         return(db.GetCollection <TItem>().AsQueryable().Where(func).ToList());
     }
 }
Beispiel #26
0
        public IQueryable <T> All <T>() where T : class, new()
        {
            //don't keep this longer than you need it.
            var db = Mongo.Create(_connectionString);

            return(db.GetCollection <T>().AsQueryable());
        }
 public static void Add <T>(T item) where T : class, new()
 {
     using (var db = Mongo.Create("_conn"))
     {
         db.GetCollection <T>().Insert(item);
     }
 }
Beispiel #28
0
 public void Save <T>(T item) where T : class, new()
 {
     using (var db = Mongo.Create(_connectionString))
     {
         db.GetCollection <T>().Save(item);
     }
 }
 public int GenerateHiloId <T>()
 {
     using (var db = Mongo.Create("_conn"))
     {
         return((int)db.GetCollection <T>().GenerateId());
     }
 }
Beispiel #30
0
        public void MapReduceWithGenericMapReduceResponse()
        {
            using (var mongo = Mongo.Create(TestHelper.ConnectionString("pooling=false")))
            {
                mongo.GetCollection <ReduceProduct>().Insert(new ReduceProduct {
                    Price = 1.5f
                },
                                                             new ReduceProduct {
                    Price = 2.5f
                },
                                                             new ReduceProduct {
                    Price = 2.5f
                });
                var mr = mongo.Database.CreateMapReduce();

                var _query   = new { Price = Q.GreaterThan(2) };
                var response =
                    mr.Execute(new MapReduceOptions <ReduceProduct> {
                    Map = _map, Reduce = _reduce, Query = _query
                });
                var collection = response.GetCollection <MapReduceResult <int, int> >();
                var r          = collection.Find().FirstOrDefault();
                Assert.Equal(0, r.Key);
                Assert.Equal(5, r.Value);
            }
        }