public void TestAddToSetWrappedNullValue()
        {
            var update   = Update.AddToSetWrapped <C>("name", null);
            var expected = "{ \"$addToSet\" : { \"name\" : null } }";

            Assert.Equal(expected, update.ToJson());
        }
        public void TestAddToSetWrapped()
        {
            var update   = Update.AddToSetWrapped("name", _a);
            var expected = "{ \"$addToSet\" : { \"name\" : { \"X\" : 1 } } }";

            Assert.Equal(expected, update.ToJson());
        }
예제 #3
0
        public void TestAddToSetEach()
        {
            var server     = Configuration.TestServer;
            var database   = Configuration.TestDatabase;
            var collection = Configuration.GetTestCollection <C>();

            collection.RemoveAll();
            var c = new C {
                InnerObjects = new List <D>()
            };

            collection.Insert(c);
            var id = c.Id;

            var query  = Query.EQ("_id", id);
            var update = Update.AddToSet("InnerObjects", 1);

            collection.Update(query, update);
            var d1 = new D {
                X = 1
            };

            update = Update.AddToSetWrapped("InnerObjects", d1);
            collection.Update(query, update);

            var d2 = new D {
                X = 2
            };
            var d3 = new D {
                X = 3
            };

            update = Update.AddToSetEachWrapped("InnerObjects", d1, d2, d3);
            collection.Update(query, update);

            var    document = collection.FindOneAs <BsonDocument>();
            var    json     = document.ToJson();
            string expected;

            if (server.BuildInfo.Version >= new Version(2, 6, 0))
            {
                expected = "{ '_id' : ObjectId('#ID'), 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }] }";
            }
            else
            {
                // prior to version 2.5.2 the server would reorder the elements when Update was used
                expected = "{ 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }], '_id' : ObjectId('#ID') }";
            }
            expected = expected.Replace("#ID", id.ToString());
            expected = expected.Replace("'", "\"");
            Assert.AreEqual(expected, json);
        }
예제 #4
0
        public void TestAddToSetEach()
        {
            var server     = Configuration.TestServer;
            var database   = Configuration.TestDatabase;
            var collection = Configuration.GetTestCollection <C>();

            collection.RemoveAll();
            var c = new C {
                InnerObjects = new List <D>()
            };

            collection.Insert(c);
            var id = c.Id;

            var query  = Query.EQ("_id", id);
            var update = Update.AddToSet("InnerObjects", 1);

            collection.Update(query, update);
            var d1 = new D {
                X = 1
            };

            update = Update.AddToSetWrapped("InnerObjects", d1);
            collection.Update(query, update);

            var d2 = new D {
                X = 2
            };
            var d3 = new D {
                X = 3
            };

            update = Update.AddToSetEachWrapped("InnerObjects", d1, d2, d3);
            collection.Update(query, update);

            var document = collection.FindOneAs <BsonDocument>();
            var json     = document.ToJson();
            var expected = "{ 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }], '_id' : ObjectId('#ID') }"; // server put _id at end?

            expected = expected.Replace("#ID", id.ToString());
            expected = expected.Replace("'", "\"");
            Assert.AreEqual(expected, json);
        }
예제 #5
0
        public void TestAddToSetEach()
        {
            var server     = MongoServer.Create("mongodb://localhost/?safe=true");
            var database   = server["onlinetests"];
            var collection = database.GetCollection <C>("csharp111");

            collection.RemoveAll();
            var c = new C {
                InnerObjects = new List <D>()
            };

            collection.Insert(c);
            var id = c.Id;

            var query  = Query.EQ("_id", id);
            var update = Update.AddToSet("InnerObjects", 1);

            collection.Update(query, update);
            var d1 = new D {
                X = 1
            };

            update = Update.AddToSetWrapped("InnerObjects", d1);
            collection.Update(query, update);

            var d2 = new D {
                X = 2
            };
            var d3 = new D {
                X = 3
            };

            update = Update.AddToSetEachWrapped("InnerObjects", d1, d2, d3);
            collection.Update(query, update);

            var document = collection.FindOneAs <BsonDocument>();
            var json     = document.ToJson();
            var expected = "{ 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }], '_id' : { '$oid' : '#ID' } }"; // server put _id at end?

            expected = expected.Replace("#ID", id.ToString());
            expected = expected.Replace("'", "\"");
            Assert.AreEqual(expected, json);
        }
예제 #6
0
        public void AddUrlToList(Url url, Guid playListId)
        {
            MongoCollection <PlayList> collection = GetPlayListForEdit();

            try
            {
                playListId = collection.FindAll().First().Id;//temporary

                //DO better solution here...
                var query = Query.EQ("UrlList.Url", url.UrlPart);
                var items = collection.Find(query).ToList();
                if (items.Count > 0)
                {
                    throw new Exception();
                }

                //collection.Update(Query.And(Query<PlayList>.EQ(e => e.Id, playListId), Query.NE("UrlList", url.UrlPart)), Update.AddToSetWrapped("UrlList", url), WriteConcern.Acknowledged);
                collection.Update(Query <PlayList> .EQ(e => e.Id, playListId), Update.AddToSetWrapped("UrlList", url));
            }
            catch (MongoCommandException ex)
            {
                string msg = ex.Message;
            }
        }
 public void TestAddToSetWrappedNullName()
 {
     Assert.Throws <ArgumentNullException>(() => Update.AddToSetWrapped(null, _a));
 }