示例#1
0
 public static void DeleteRecords(DBCollection dbc, BsonDocument condition, BsonDocument hint)
 {
     try
     {
         dbc.Delete(condition, hint);
     }
     catch (BaseException e)
     {
         Console.WriteLine("Failed to delete records from collection {0}, ErrorType = {1}", dbc.Name, e.ErrorType);
         Environment.Exit(0);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         Environment.Exit(0);
     }
 }
示例#2
0
        public void Transaction_Begin_Commit_delete_Test()
        {
            // create cs, cl
            string csName = "testfoo";
            string cName  = "testbar";

            if (sdb.IsCollectionSpaceExist(csName))
            {
                sdb.DropCollectionSpace(csName);
            }
            sdb.CreateCollectionSpace(csName);
            CollectionSpace cs = sdb.GetCollecitonSpace(csName);
            DBCollection    cl = cs.CreateCollection(cName);
            // insert record
            BsonDocument insertor1 = new BsonDocument();

            insertor1.Add("name", "tom");
            insertor1.Add("age", 25);
            insertor1.Add("addr", "guangzhou");
            BsonDocument insertor2 = new BsonDocument();

            insertor2.Add("name", "sam");
            insertor2.Add("age", 27);
            insertor2.Add("addr", "shanghai");
            cl.Insert(insertor1);
            cl.Insert(insertor2);
            // transction begin
            sdb.TransactionBegin();
            // delete
            BsonDocument matcher = new BsonDocument();

            //matcher.Add("name", new BsonDocument("$et","sam"));
            matcher.Add("name", "sam");
            cl.Delete(matcher);
            // check up
            DBCursor cursor = cl.Query();

            Assert.IsNotNull(cursor);
            int count = 0;

            while (cursor.Next() != null)
            {
                ++count;
                BsonDocument bson = cursor.Current();
                Assert.IsNotNull(bson);
            }
            Assert.IsTrue(count == 1);
            // commit
            sdb.TransactionCommit();
            // check up
            cursor = cl.Query(matcher, null, null, null);
            Assert.IsNotNull(cursor);
            count = 0;
            while (cursor.Next() != null)
            {
                ++count;
                BsonDocument bson = cursor.Current();
                Assert.IsNotNull(bson);
            }
            Assert.IsTrue(count == 0);
            // chech up
            cursor = cl.Query();
            Assert.IsNotNull(cursor);
            count = 0;
            while (cursor.Next() != null)
            {
                ++count;
                BsonDocument bson = cursor.Current();
                Assert.IsNotNull(bson);
            }
            Assert.IsTrue(count == 1);
        }
        public void InsertTest_WithId()
        {
            int testTypeSize = 8;

            for (int i = 0; i < testTypeSize; i++)
            {
                // insert
                BsonDocument insertor = new BsonDocument();
                string       date     = DateTime.Now.ToString();
                insertor.Add("operation", "Insert");
                insertor.Add("date", date);
                switch (i)
                {
                case 0:
                    insertor.Add("_id", 3.14);
                    break;

                case 1:
                    insertor.Add("_id", "abcdefg");
                    break;

                case 2:
                    insertor.Add("_id", new BsonDocument("id", "id"));
                    break;

                case 3:
                    insertor.Add("_id", ObjectId.GenerateNewId());
                    break;

                case 4:
                    insertor.Add("_id", true);
                    break;

                case 5:
                    insertor.Add("_id", 1234);
                    break;

                case 6:
                    insertor.Add("_id", 10000L);
                    break;

                case 7:
                    insertor.Add("_id", new BsonTimestamp(1000000000L));
                    break;

                default:
                    continue;
                }
                coll.Delete(null);
                BsonValue value = coll.Insert(insertor);
                Object    id    = null;
                BsonType  type  = value.BsonType;
                if (type == BsonType.Double)
                {
                    id = value.AsDouble;
                }
                else if (type == BsonType.String)
                {
                    id = value.AsString;
                }
                else if (type == BsonType.Document)
                {
                    id = value.AsBsonDocument;
                }
                else if (type == BsonType.Array)
                {
                    id = value.AsBsonArray;
                }
                else if (type == BsonType.Binary)
                {
                    id = value.AsBsonBinaryData;
                }
                else if (type == BsonType.Undefined)
                {
                    id = value.AsBsonUndefined;
                }
                else if (type == BsonType.ObjectId)
                {
                    id = value.AsObjectId;
                }
                else if (type == BsonType.Boolean)
                {
                    id = value.AsBoolean;
                }
                else if (type == BsonType.DateTime)
                {
                    id = value.AsDateTime;
                }
                else if (type == BsonType.Null)
                {
                    ;
                }
                else if (type == BsonType.RegularExpression)
                {
                    id = value.AsRegex;
                }
                else if (type == BsonType.JavaScript)
                {
                    id = value.AsBsonJavaScript;
                }
                else if (type == BsonType.Symbol)
                {
                    id = value.AsBsonSymbol;
                }
                else if (type == BsonType.JavaScriptWithScope)
                {
                    id = value.AsBsonJavaScriptWithScope;
                }
                else if (type == BsonType.Int32)
                {
                    id = value.AsInt32;
                }
                else if (type == BsonType.Timestamp)
                {
                    id = value.AsBsonTimestamp;
                }
                else if (type == BsonType.Int64)
                {
                    id = value.AsInt64;
                }
                else if (type == BsonType.MinKey)
                {
                    id = value.AsBsonMinKey;
                }
                else if (type == BsonType.MaxKey)
                {
                    id = value.AsBsonMaxKey;
                }

                BsonDocument matcher = new BsonDocument();
                DBQuery      query   = new DBQuery();
                matcher.Add("date", date);
                query.Matcher = matcher;
                DBCursor cursor = coll.Query(query);
                Assert.IsNotNull(cursor);
                BsonDocument bson = cursor.Next();
                Assert.IsNotNull(bson);

                BsonValue ret = bson.GetValue("_id");
                type = ret.BsonType;
                if (type == BsonType.Double)
                {
                    Assert.IsTrue(id.Equals(ret.AsDouble));
                }
                else if (type == BsonType.String)
                {
                    Assert.IsTrue(id.Equals(ret.AsString));
                }
                else if (type == BsonType.Document)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonDocument));
                }
                else if (type == BsonType.Array)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonArray));
                }
                else if (type == BsonType.Binary)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonBinaryData));
                }
                else if (type == BsonType.Undefined)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonUndefined));
                }
                else if (type == BsonType.ObjectId)
                {
                    Assert.IsTrue(id.Equals(ret.AsObjectId));
                }
                else if (type == BsonType.Boolean)
                {
                    Assert.IsTrue(id.Equals(ret.AsBoolean));
                }
                else if (type == BsonType.DateTime)
                {
                    Assert.IsTrue(id.Equals(ret.AsDateTime));
                }
                else if (type == BsonType.Null)
                {
                    Assert.IsTrue(id == null);
                }
                else if (type == BsonType.RegularExpression)
                {
                    Assert.IsTrue(id.Equals(ret.AsRegex));
                }
                else if (type == BsonType.JavaScript)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonJavaScript));
                }
                else if (type == BsonType.Symbol)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonSymbol));
                }
                else if (type == BsonType.JavaScriptWithScope)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonJavaScriptWithScope));
                }
                else if (type == BsonType.Int32)
                {
                    Assert.IsTrue(id.Equals(ret.AsInt32));
                }
                else if (type == BsonType.Timestamp)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonTimestamp));
                }
                else if (type == BsonType.Int64)
                {
                    Assert.IsTrue(id.Equals(ret.AsInt64));
                }
                else if (type == BsonType.MinKey)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonMinKey));
                }
                else if (type == BsonType.MaxKey)
                {
                    Assert.IsTrue(id.Equals(ret.AsBsonMaxKey));
                }
            }
        }
示例#4
0
 /// <summary>
 /// Removes the specified collection.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="collection">The collection.</param>
 /// <param name="selector">The selector.</param>
 public static void Delete <T>(this DBCollection <T> collection, Expression <Func <T, bool> > selector) where T : class
 {
     collection.Delete((BsonDocument)GetQuery(collection, selector));
 }
示例#5
0
        /// <summary>
        /// Update测试。
        /// </summary>
        /// <param name="sdb"></param>
        static void TestUpdate(Sequoiadb sdb)
        {
            // The collection space name
            string csName = "sample";
            // The collection name
            string cName = "sample";

            // connect
            CollectionSpace cs;

            if (sdb.IsCollectionSpaceExist(csName))
            {
                cs = sdb.GetCollecitonSpace(csName);
            }
            else
            {
                cs = sdb.CreateCollectionSpace(csName);
            }

            DBCollection coll = null;

            if (cs.IsCollectionExist(cName))
            {
                coll = cs.GetCollection(cName);
            }
            else
            {
                coll = cs.CreateCollection(cName);
            }

            // delete all records from the collection
            BsonDocument bson = new BsonDocument();

            coll.Delete(bson);

            String[] record = new String[4];
            record[0] = "{cust_id:\"A123\",amount:500,status:\"A\"}";
            record[1] = "{cust_id:\"A123\",amount:250,status:\"A\"}";
            record[2] = "{cust_id:\"B212\",amount:200,status:\"A\"}";
            record[3] = "{cust_id:\"A123\",amount:300,status:\"D\"}";
            // insert record into database
            for (int i = 0; i < record.Length; i++)
            {
                BsonDocument obj = new BsonDocument();
                obj = BsonDocument.Parse(record[i]);
                Console.WriteLine("Record is: " + obj.ToString());
                coll.Insert(obj);
            }

            //准备update
            BsonDocument updater  = new BsonDocument();
            BsonDocument matcher  = new BsonDocument();
            BsonDocument modifier = new BsonDocument();
            BsonDocument hint     = new BsonDocument();

            //条件
            matcher.Add("cust_id", new BsonDocument("$et", "A123"));
            //更新。
            updater.Add("amount", "1000");
            updater.Add("status", "C");
            modifier.Add("$set", updater);
            //update
            coll.Update(matcher, modifier, hint);

            System.Console.ReadLine();
        }
示例#6
0
        static void TestAggregate5(Sequoiadb sdb)
        {
            // The collection space name
            string csName = "sample";
            // The collection name
            string cName = "sample";

            // connect
            CollectionSpace cs;

            if (sdb.IsCollectionSpaceExist(csName))
            {
                cs = sdb.GetCollecitonSpace(csName);
            }
            else
            {
                cs = sdb.CreateCollectionSpace(csName);
            }

            DBCollection coll = null;

            if (cs.IsCollectionExist(cName))
            {
                coll = cs.GetCollection(cName);
            }
            else
            {
                coll = cs.CreateCollection(cName);
            }

            // delete all records from the collection
            BsonDocument bson = new BsonDocument();

            coll.Delete(bson);

            String[] command = new String[2];
            command[0] = "{$match:{status:\"A\"}}";
            command[1] = "{$group:{_id:\"$cust_id\",amount:{\"$sum\":\"$amount\"},cust_id:{\"$first\":\"$cust_id\"}}}";
            String[] record = new String[4];
            record[0] = "{cust_id:\"A123\",amount:500,status:\"A\"}";
            record[1] = "{cust_id:\"A123\",amount:250,status:\"A\"}";
            record[2] = "{cust_id:\"B212\",amount:200,status:\"A\"}";
            record[3] = "{cust_id:\"A123\",amount:300,status:\"D\"}";
            // insert record into database
            for (int i = 0; i < record.Length; i++)
            {
                BsonDocument obj = new BsonDocument();
                obj = BsonDocument.Parse(record[i]);
                Console.WriteLine("Record is: " + obj.ToString());
                coll.Insert(obj);
            }
            List <BsonDocument> list = new List <BsonDocument>();

            for (int i = 0; i < command.Length; i++)
            {
                BsonDocument obj = new BsonDocument();
                obj = BsonDocument.Parse(command[i]);
                list.Add(obj);
            }

            DBCursor cursor = coll.Aggregate(list);
            int      count  = 0;

            while (null != cursor.Next())
            {
                Console.WriteLine("Result is: " + cursor.Current().ToString());
                String str = cursor.Current().ToString();
                count++;
            }

            System.Console.ReadLine();
        }