public void QueryTest()
 {
     for (int i = 0; i < 10; ++i)
     {
         string date = DateTime.Now.ToString();
         BsonDocument insertor = new BsonDocument();
         insertor.Add("operation", "Query");
         insertor.Add("date", date);
         coll.Insert(insertor);
     }
     BsonDocument matcher = new BsonDocument();
     DBQuery query = new DBQuery();
     matcher.Add("operation", "Query");
     query.Matcher = matcher;
     query.ReturnRowsCount = 5;
     query.SkipRowsCount = 5;
     DBCursor cursor = coll.Query(query);
     Assert.IsNotNull(cursor);
     int count = 0;
     while (cursor.Next() != null)
     {
         ++count;
         BsonDocument bson = cursor.Current();
         Assert.IsNotNull(bson);
     }
     Assert.IsTrue(count == 5);
 }
        public void UpsertTest()
        {
            BsonDocument updater = new BsonDocument();
            BsonDocument matcher = new BsonDocument();
            BsonDocument modifier = new BsonDocument();
            BsonDocument hint = new BsonDocument();
            BsonDocument tempMatcher = new BsonDocument();
            DBQuery query = new DBQuery();
            matcher.Add("operation", new BsonDocument("$et", "Upsert"));
            query.Matcher = matcher;

            long count = coll.GetCount(matcher);
            Assert.IsTrue(count == 0);

            // update but insert
            updater.Add("operation", "Upsert");
            updater.Add("type", "Insert");
            modifier.Add("$set", updater);
            tempMatcher.Add("type",new BsonDocument("$et","Insert"));
            coll.Upsert(matcher, modifier, hint);
            //coll.Upsert(matcher, null, null);
            count = coll.GetCount(tempMatcher);
            Assert.IsTrue(count == 1);
            DBCursor cursor = coll.Query(query);
            Assert.IsNotNull(cursor);
            BsonDocument rtn = cursor.Next();
            Assert.IsNotNull(rtn);
            Assert.IsTrue(rtn["type"].AsString.Equals("Insert"));

            // update
            updater = new BsonDocument();
            updater.Add("type", "Update");
            modifier = new BsonDocument();
            modifier.Add("$set", updater);
            coll.Upsert(matcher, modifier, hint);

            cursor = coll.Query(query);
            Assert.IsNotNull(cursor);
            count = coll.GetCount(matcher);
            Assert.IsTrue(count == 1);
            rtn = cursor.Next();
            Assert.IsTrue(rtn["type"].AsString.Equals("Update"));
        }
        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));
            }
        }
        public void OperationTest()
        {
            // Insert
            BsonDocument insertor = new BsonDocument();
            insertor.Add("Last Name", "Lin");
            insertor.Add("First Name", "Hetiu");
            insertor.Add("Address", "SYSU");
            BsonDocument sInsertor = new BsonDocument();
            sInsertor.Add("Phone", "10086");
            sInsertor.Add("EMail", "*****@*****.**");
            insertor.Add("Contact", sInsertor);
            ObjectId insertID = (ObjectId)coll.Insert(insertor);
            Assert.IsNotNull(insertID);

            // Update
            DBQuery query = new DBQuery();
            BsonDocument updater = new BsonDocument();
            BsonDocument matcher = new BsonDocument();
            BsonDocument modifier = new BsonDocument();
            updater.Add("Age", 25);
            modifier.Add("$set", updater);
            matcher.Add("First Name", "Hetiu");
            query.Matcher = matcher;
            query.Modifier = modifier;
            coll.Update(query);

            // Query
            DBCursor cursor = coll.Query(query);
            Assert.IsNotNull(cursor);
            BsonDocument bson = cursor.Next();
            Assert.IsNotNull(bson);
            Assert.IsTrue(bson["First Name"].AsString.Equals("Hetiu"));
            Assert.IsTrue(bson["Age"].AsInt32.Equals(25));

            // Delete
            BsonDocument drop = new BsonDocument();
            drop.Add("Last Name", "Lin");
            coll.Delete(drop);
            query.Matcher = drop;
            cursor = coll.Query(query);
            Assert.IsNotNull(cursor);
            bson = cursor.Next();
            Assert.IsNull(bson);
        }
        public void InsertChineseTest()
        {
            BsonDocument insertor = new BsonDocument();
            insertor.Add("姓名", "林海涛");
            insertor.Add("年龄", 24);
            insertor.Add("id", 2001);

            // an embedded bson object
            BsonDocument phone = new BsonDocument
                {
                    {"0", "10086"},
                    {"1", "10000"}
                };

            insertor.Add("电话", phone);

            ObjectId id = (ObjectId)coll.Insert(insertor);

            BsonDocument matcher = new BsonDocument();
            DBQuery query = new DBQuery();
            matcher.Add("姓名", "林海涛");
            query.Matcher = matcher;
            DBCursor cursor = coll.Query(query);
            Assert.IsNotNull(cursor);
            BsonDocument rtn = cursor.Next();
            Assert.IsNotNull(rtn);
            Assert.IsTrue(id.Equals(rtn["_id"].AsObjectId));
        }
        public void InsertTest()
        {
            // insert
            BsonDocument insertor = new BsonDocument();
            string date = DateTime.Now.ToString();
            insertor.Add("operation", "Insert");
            insertor.Add("date", date);
            Object id = (ObjectId)coll.Insert(insertor);

            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);
            Assert.IsTrue(id.Equals(bson["_id"].AsObjectId));
            //Assert.IsTrue(id.)
        }
Beispiel #7
0
 /** \fn void Update(DBQuery query)
  *  \brief Update the document of current collection
  *  \param query DBQuery with matching condition, updating rule and hint
  *  \exception SequoiaDB.BaseException
  *  \exception System.Exception
  *  \note It won't work to update the "ShardingKey" field, but the other fields take effect
  */
 public void Update(DBQuery query)
 {
     _Update(0, query.Matcher, query.Modifier, query.Hint);
 }
Beispiel #8
0
        /** \fn DBCursor Query(DBQuery query)
         *  \brief Find documents of current collection with DBQuery
         *  \param query DBQuery with matching condition, selector, order rule, hint, SkipRowsCount and ReturnRowsCount
         *  \return The DBCursor of matching documents or null
         *  \exception SequoiaDB.BaseException
         *  \exception System.Exception
         */
        public DBCursor Query(DBQuery query)
        {
            BsonDocument dummyObj = new BsonDocument();
            BsonDocument matcher = query.Matcher;
            BsonDocument selector = query.Selector;
            BsonDocument orderBy = query.OrderBy;
            BsonDocument hint = query.Hint;
            long skipRows = query.SkipRowsCount;
            long returnRows = query.ReturnRowsCount;
            int flag = query.Flag;

            return Query(matcher, selector, orderBy, hint, skipRows, returnRows, flag);
        }
Beispiel #9
0
        static void FindSortedRecords(DBCollection dbc)
        {
            DBQuery query = new DBQuery();
            try
            {
                BsonDocument condition = new BsonDocument
                {
                    {"Id",
                        new BsonDocument
                        {
                            {"$gte", 50},
                            {"$lte", 70}
                        }
                    }
                };

                query.Matcher = condition;
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create query condition");
                Console.WriteLine(e.Message);
                return;
            }

            try
            {
                BsonDocument orderBy = new BsonDocument { { "id", -1 } };

                query.OrderBy = orderBy;
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create orderBy clause");
                Console.WriteLine(e.Message);
                return;
            }

            Console.WriteLine("Find ordered data ( by \"id\" in DESC order ) from Collection");
            Common.FindRecord(dbc, query);
        }
Beispiel #10
0
        static void FindRecordsUsingHintIXScan(DBCollection dbc)
        {
            DBQuery query = new DBQuery();
            try
            {
                BsonDocument condition = new BsonDocument
                {
                    {"Id",
                        new BsonDocument
                        {
                            {"$gte", 50},
                            {"$lte", 70}
                        }
                    }
                };

                query.Matcher = condition;
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create query condition");
                Console.WriteLine(e.Message);
                return;
            }

            try
            {
                // provide index name for index scan
                BsonDocument hint = new BsonDocument { { "", indexName } };

                query.Hint = hint;
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create hint");
                Console.WriteLine(e.Message);
                return;
            }

            Console.WriteLine("Find records using index scan");
            Common.FindRecord(dbc, query);
        }
Beispiel #11
0
        static void FindRecordsFields(DBCollection dbc)
        {
            DBQuery query = new DBQuery();
            try
            {
                BsonDocument condition = new BsonDocument
                {
                    {"Id",
                        new BsonDocument
                        {
                            {"$gte", 50},
                            {"$lte", 70}
                        }
                    }
                };

                query.Matcher = condition;
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create query condition");
                Console.WriteLine(e.Message);
                return;
            }

            try
            {
                BsonDocument selector = new BsonDocument
                {
                    {"id", ""},
                    {"FirstName", ""},
                    {"LastName", ""},
                    {"PhoneNumber", ""}
                };

                query.Selector = selector;
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create fields selector");
                Console.WriteLine(e.Message);
                return;
            }

            Console.WriteLine("Find specific fields from Collection");
            Common.FindRecord(dbc, query);
        }
Beispiel #12
0
        static void FindRangeRecords(DBCollection dbc)
        {
            DBQuery query = new DBQuery();
            try
            {
                BsonDocument condition = new BsonDocument
                {
                    {"Id",
                        new BsonDocument
                        {
                            {"$gte", 25},
                            {"$lte", 30}
                        }
                    }
                };

                query.Matcher = condition;
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create range record query condition");
                Console.WriteLine(e.Message);
                return;
            }

            Console.WriteLine("Find range records from collection");
            Common.FindRecord(dbc, query);
        }
Beispiel #13
0
        public static void FindRecord(DBCollection dbc, DBQuery query)
        {
            try
            {
                // find specific records from collection with DBQuery
                DBCursor cursor = dbc.Query(query);

                Console.WriteLine("Record read:");
                while ( cursor.Next() != null )
                    Console.WriteLine(cursor.Current().ToString());
            }
            catch (BaseException e)
            {
                Console.WriteLine("Failed to find records from collection, ErrorType = {0}", e.ErrorType);
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(0);
            }
        }