public void Update_EmptyDb_UpsertsDocument()
        {
            var document = new { Name = "Daniel" };

            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                session[DbName][PersonsCollectionName].Update(document, document);
            }

            var numOfStoredDocuments = TestHelper.GetDocumentCount(Constants.Collections.PersonsCollectionName);
            Assert.AreEqual(1, numOfStoredDocuments);
        }
        public void Insert_TypedDocumentWithAutoAssignedId_IsStored()
        {
            var document = new PersonWithAutoId { Name = "Daniel" };

            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                session[DbName][PersonsCollectionName].Insert(document);
            }

            var numOfDocs = TestHelper.GetDocumentCount(new { document._id }, Constants.Collections.PersonsCollectionName);
            Assert.AreEqual(1, numOfDocs);
        }
Exemplo n.º 3
0
        public void Insert_Example()
        {
            //The hierarchy in MongoDB is:
            //- Db
            //  - Collection
            //    - Document
            //By using EntityStore this is somewhat abstracted away.

            //You do need a connection to create a session
            //The creating of Connections and Session is something you most likely
            //will put in a IoC-container or factory or something.
            var cn = TestHelper.CreateConnection();
            using (var session = new SimoSession(cn))
            {
                var entityStore = new SimoEntityStore(session, DbName);

                //If you are dealing with typed documents (e.g. Person)
                //you can use the generic API.
                //Then the name of the collection is the name of the
                //type, but pluralized.
                //If you don't want pluralization, call
                //session.SimoPluralizer.Disable(), or
                //replace the implementation of the
                //SimoIoC.Instance.PluralizerResolver
                var person = new Person { Name = "Daniel", Age = 29 };
                entityStore.Insert(person);

                //If you are using non-typed documents you have to pass
                //the entityname string to the method you are using.
                var anonymousPerson = new { Name = "Daniel" };
                entityStore.Insert("Person", anonymousPerson);

                //So the EntityStore only allows for an abstraction
                //over the Db, Collection and Document hierarchy
                //and you can of course access these to.
                var db = session[DbName];
                var persons = db["Person"];
                persons.Insert(anonymousPerson);

                //The EntityStore also holds the Database that it wraps.
                //Of course you can obtain a collection using generics against the
                //database.
                var persons2 = entityStore.Database.GetCollection<Person>();
                persons2.Insert(person);

                var numOfStoredDocuments = entityStore.Count<Person>();
                Assert.AreEqual(4, numOfStoredDocuments);
            }
        }
        public void Insert_ManyDocuments_AllAreStored()
        {
            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                var documents = new[]
                                {
                                    new Person {Name = "Daniel", Age = 29, WorkDays = new[] {1, 1, 1, 0, 1, 0, 0}},
                                    new Person {Name = "Lazy John", Age = 55, WorkDays = new[] {0, 1, 0, 1, 0, 0, 0}}
                                };
                session[DbName][PersonsCollectionName].Insert(documents);
            }

            var numOfStoredDocuments = TestHelper.GetDocumentCount(Constants.Collections.PersonsCollectionName);
            Assert.AreEqual(2, numOfStoredDocuments);
        }
Exemplo n.º 5
0
        public void FindOne_ChildHasNullReferenceToParent_ReturnsChildWithNullReferenceParent()
        {
            var child = new Child { Name = "Isabell" };
            TestHelper.InsertDocument(Constants.Collections.ChildsCollectionName, child);

            var cn = TestHelper.CreateConnection();
            using (var session = new SimoSession(cn))
            {
                var entityStore = new SimoEntityStore(session, DbName);

                var refetchedChild = entityStore.FindOne<Child>(new { _id = child._id });

                Assert.IsNotNull(refetchedChild, "Couldn't refetch child.");
                Assert.IsNull(refetchedChild.FatherReference, "Fatherreference should be null.");
            }
        }
        public void Insert_SingleDocument_IsStored()
        {
            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                var document = new
                               {
                                   Name = "Daniel",
                                   BirthDay = new DateTime(1980, 05, 03),
                                   WorkDays = new[] { 1, 1, 1, 0, 1, 0, 0 }
                               };
                session[DbName][PersonsCollectionName].Insert(document);
            }

            var numOfStoredDocuments = TestHelper.GetDocumentCount(Constants.Collections.PersonsCollectionName);
            Assert.AreEqual(1, numOfStoredDocuments);
        }
Exemplo n.º 7
0
        public void Find_UsingDocumentSelector_SingleItemReturned()
        {
            var documents = new[]
                            {
                                new Person {Name = "Daniel", Age = 29, WorkDays = new[] {1, 1, 1, 0, 1, 0, 0}},
                                new Person {Name = "Lazy John", Age = 55, WorkDays = new[] {0, 1, 0, 1, 0, 0, 0}},
                                new Person {Name = "Nobel Adam", Age = 65, WorkDays = new[] {1, 1, 1, 1, 1, 1, 1}},
                                new Person {Name = "Sue", Age = 20, WorkDays = new[] {1, 1, 1, 1, 1, 0, 0}}
                            };
            TestHelper.InsertDocuments(Constants.Collections.PersonsCollectionName, documents);

            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                var persons = session[DbName][PersonsCollectionName].Find<Person>(new { WorkDays = new[] { 1, 1, 1, 1, 1, 1, 1 } });

                Assert.AreEqual("Nobel Adam", persons[0].Name);
            }
        }
Exemplo n.º 8
0
        public void Find_UsingAllOperator_ReturnsTwoMatchingPosts()
        {
            var collectionName = "BlogEntries";
            var documents = new[]
            {
                new { Title = "MongoDb other", Tags = new[] { "MongoDb", "Misc" } },
                new { Title = "MongoDb and testing tutorial", Tags = new[] { "MongoDb", "Testing", "Tutorial" } },
                new { Title = "MongoDb tutorial", Tags = new[] { "MongoDb", "Tutorial" } }
            };
            TestHelper.InsertDocuments(collectionName, documents);

            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                var tutorials = session[DbName][collectionName].FindInfered(new { Title = "" }, @"{Tags : {$all :['Tutorial']}}");

                Assert.AreEqual(2, tutorials.Count);
            }
        }
Exemplo n.º 9
0
        public void Find_UsingJsonInOperator_TwoOfThreeItemsReturned()
        {
            var documents = new[]
                            {
                                new Person {Name = "Daniel", Age = 29},
                                new Person {Name = "Adam", Age = 55},
                                new Person {Name = "Sue", Age = 55},
                            };
            TestHelper.InsertDocuments(Constants.Collections.PersonsCollectionName, documents);

            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                var persons = session[DbName][PersonsCollectionName].Find<Person>(@"{Name : { $in : [""Daniel"", ""Sue""] } }");

                var danielAndSueFound = persons.Where(p => new[] { "Daniel", "Sue" }.Contains(p.Name)).Count() == 2;
                Assert.AreEqual(2, persons.Count);
                Assert.IsTrue(danielAndSueFound);
            }
        }
Exemplo n.º 10
0
        public void Find_UsingRegex()
        {
            var cn = TestHelper.CreateConnection();
            using (var session = new SimoSession(cn))
            {
                var entityStore = new SimoEntityStore(session, DbName);

                entityStore.Insert(new Person { Name = "Daniel", Age = 29 });
                entityStore.Insert(new Person { Name = "Daniel1", Age = 45 });
                entityStore.Insert(new Person { Name = "Daniel1", Age = 55 });
                entityStore.Insert(new Person { Name = "Daniel2", Age = 65 });
                entityStore.Insert(new Person { Name = "Sue", Age = 20 });

                var refetched = entityStore.FindOne<Person>(new { Age = 45, Name = new SimoRegex("^Dan.*1") });

                Assert.AreEqual(45, refetched.Age);
                Assert.AreEqual("Daniel1", refetched.Name);
            }
        }
Exemplo n.º 11
0
        public void DisablePluralizer_Example()
        {
            var cn = TestHelper.CreateConnection();
            using (var session = new SimoSession(cn))
            {
                //Just call Disable on the Pluralizer
                //When you want to enable it again, call
                //Enable().
                var entityStore = new SimoEntityStore(session, DbName);
                entityStore.Session.Pluralizer.Disable();

                var person = new Person { Name = "Daniel" };
                entityStore.Insert(person);

                var refetched = entityStore.Database["Person"].FindOne<Person>(new { person.Name });

                Assert.IsNotNull(refetched);
            }
        }
Exemplo n.º 12
0
        public void Delete_UsingJsonSelector_TwoOfFourDocumentsAreDeleted()
        {
            var documents = new[]
                            {
                                new Person {Name = "Daniel", Age = 29, WorkDays = new[] {1, 1, 1, 0, 1, 0, 0}},
                                new Person {Name = "Lazy John", Age = 55, WorkDays = new[] {0, 1, 0, 1, 0, 0, 0}},
                                new Person {Name = "Nobel Adam", Age = 65, WorkDays = new[] {1, 1, 1, 1, 1, 1, 1}},
                                new Person {Name = "Sue", Age = 20, WorkDays = new[] {1, 1, 1, 1, 1, 0, 0}}
                            };
            TestHelper.InsertDocuments(Constants.Collections.PersonsCollectionName, documents);

            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                session[DbName][PersonsCollectionName].Delete(@"{$where : ""this.Name == 'Sue' || this.WorkDays[0] == 0""}");
            }

            var storedDocuments = TestHelper.GetDocuments<Person>(Constants.Collections.PersonsCollectionName);
            Assert.AreEqual("Daniel", storedDocuments[0].Name);
            Assert.AreEqual("Nobel Adam", storedDocuments[1].Name);
        }
        public void Find_UsingJsonWhereOperator_ReturnsTwoOfThree()
        {
            var documents = new[]
                            {
                                new Person {Name = "Daniel", Age = 29},
                                new Person {Name = "Adam", Age = 55},
                                new Person {Name = "Sue", Age = 55},
                            };
            TestHelper.InsertDocuments(Constants.Collections.PersonsCollectionName, documents);

            var cn = TestHelper.CreateConnection();
            using (var session = new SimoSession(cn))
            {
                var entityStore = new SimoEntityStore(session, DbName);

                var persons = entityStore.Find<Person>(@"{$where : ""this.Name == 'Daniel' || this.Name == 'Sue'""}");

                var danielAndSueFound = persons.Where(p => new[] { "Daniel", "Sue" }.Contains(p.Name)).Count() == 2;
                Assert.AreEqual(2, persons.Count);
                Assert.IsTrue(danielAndSueFound);
            }
        }
        public void Update_UsingAnonymousDocument_TwoOfFourItemsUpdated()
        {
            var documents = new[]
                            {
                                new Person {Name = "Daniel1", Age = 29},
                                new Person {Name = "Daniel2", Age = 30},
                                new Person {Name = "Daniel3", Age = 31},
                                new Person {Name = "Daniel4", Age = 32}
                            };
            TestHelper.InsertDocuments(Constants.Collections.PersonsCollectionName, documents);

            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                session[DbName][PersonsCollectionName].UpdateMany(@"{$where : ""this.Age < 31""}", @"{$inc : {Age : 1}}");
            }

            var refetched = TestHelper.GetDocuments<Person>(Constants.Collections.PersonsCollectionName);
            Assert.AreEqual(30, refetched[0].Age);
            Assert.AreEqual(31, refetched[1].Age);
            Assert.AreEqual(31, refetched[2].Age);
            Assert.AreEqual(32, refetched[3].Age);
        }
Exemplo n.º 15
0
        public void Find_UsingWhereOperator_SingleItemReturned()
        {
            var documents = new[]
                            {
                                new Person {Name = "Daniel", Age = 29},
                                new Person {Name = "Lazy John", Age = 55},
                                new Person {Name = "Nobel Adam", Age = 65},
                                new Person {Name = "Sue", Age = 20}
                            };
            TestHelper.InsertDocuments(Constants.Collections.PersonsCollectionName, documents);

            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                var persons = session[DbName][PersonsCollectionName].Find<Person>(Query.New(q => q.Where("this.Age > 20 && this.Age < 65")));

                Assert.AreEqual("Daniel", persons[0].Name);
                Assert.AreEqual("Lazy John", persons[1].Name);
            }
        }
Exemplo n.º 16
0
        public void Find_UsingRegExOperator_ThreeItemsReturned()
        {
            var documents = new[]
                            {
                                new Person {Name = "Daniel", Age = 29},
                                new Person {Name = "Daniel1", Age = 55},
                                new Person {Name = "Daniel2", Age = 65},
                                new Person {Name = "Sue", Age = 20}
                            };
            TestHelper.InsertDocuments(Constants.Collections.PersonsCollectionName, documents);

            using (var session = new SimoSession(TestHelper.CreateConnection()))
            {
                var persons = session[DbName][PersonsCollectionName].Find<Person>(new { Name = new SimoRegex("^Dan.*") });

                Assert.AreEqual(3, persons.Count, "Wrong number of persons returned.");
                Assert.AreEqual("Daniel", persons[0].Name);
                Assert.AreEqual("Daniel1", persons[1].Name);
                Assert.AreEqual("Daniel2", persons[2].Name);
            }
        }
Exemplo n.º 17
0
        public void Find_UsingMatchingValueInArray_ReturnsMatchingPost()
        {
            var collectionName = "BlogEntries";
            var documents = new[]
            {
                new { Title = "MongoDb and testing", Tags = new[] { "MongoDb", "Testing" } },
                new { Title = "MongoDb tutorial", Tags = new[] { "MongoDb", "Tutorial" } }
            };
            TestHelper.InsertDocuments(collectionName, documents);

            using(var session = new SimoSession(TestHelper.CreateConnection()))
            {
                var tutorial = session[DbName][collectionName].FindOneInfered(new {Title = ""}, @"{Tags : 'Tutorial'}");

                Assert.AreEqual("MongoDb tutorial", tutorial.Title);
            }
        }
Exemplo n.º 18
0
        public void ParentChildReference_CustomId_Example()
        {
            var cn = TestHelper.CreateConnection();
            using (var session = new SimoSession(cn))
            {
                var entityStore = new SimoEntityStore(session, DbName);

                //This time we use anonymous type and custom Id's (Guids).
                var parent = new { _id = Guid.NewGuid(), Name = "Daniel" };
                var fatherReference = entityStore.Reference("Parent", parent._id);
                var child = new { _id = Guid.NewGuid(), Name = "Isabell", FatherReference = fatherReference };

                //You could of course have created the reference manually, but then you loose the
                //use of the pluralizer, and have to role-this on your own.
                //new SimoReference<Guid> { CollectionName = "Parents", Id = parent._id };

                entityStore.Insert("Parent", parent);
                entityStore.Insert("Child", child);

                var refetchedChild = entityStore.FindOneInfered(child, entityName: "Child", selector: new { child._id });

                Assert.AreEqual(fatherReference.Id, refetchedChild.FatherReference.Id);
                Assert.AreEqual(fatherReference.CollectionName, refetchedChild.FatherReference.CollectionName);
            }
        }
Exemplo n.º 19
0
        public void ParentChildReference_Example()
        {
            var cn = TestHelper.CreateConnection();
            using (var session = new SimoSession(cn))
            {
                var entityStore = new SimoEntityStore(session, DbName);

                //The parent generates a new _id when created.
                //That _id is then used in the reference which is attached to the child.
                //After that, you just store the items.
                var parent = new Parent { Name = "Daniel" };
                var fatherReference = entityStore.Reference<Parent>(parent._id);
                var child = new Child { Name = "Isabell", FatherReference = fatherReference };

                //You could of course have created the reference manually, but then you loose the
                //use of the pluralizer, and have to role-this on your own.
                //new SimoReference { CollectionName = "Parents", Id = parent._id };

                entityStore.Insert(parent);
                entityStore.Insert(child);

                var refetchedChild = entityStore.FindOne<Child>(new { child._id });

                Assert.AreEqual(fatherReference.Id, refetchedChild.FatherReference.Id);
                Assert.AreEqual(fatherReference.CollectionName, refetchedChild.FatherReference.CollectionName);
            }
        }