static void TestAggregate2(Sequoiadb sdb) { var cs = sdb.GetCollecitonSpace("dbo"); DBCollection coll = null; if (cs.IsCollectionExist("t2")) { coll = cs.GetCollection("t2"); } else { coll = cs.CreateCollection("t2"); } 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++; } }
public void AggregateTest() { String[] command = new String[2]; command[0] = "{$match:{status:\"A\"}}"; command[1] = "{$group:{_id:\"$cust_id\",total:{$sum:\"$amount\"}}}"; 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++; } Assert.IsTrue(2 == count); }
public static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Please give the database server address <IP:Port>"); Environment.Exit(0); } // The database server address string sdbIP = args[0]; // The collection space name string csName = "sample"; // The collection name string cName = "sample"; Sequoiadb sdb = new Sequoiadb(sdbIP); // connect Common.Connect(sdb); CollectionSpace cs = Common.GetCollecitonSpace(sdb, csName); DBCollection coll = Common.GetColleciton(cs, cName); // delete all records from the collection BsonDocument bson = new BsonDocument(); Common.DeleteRecords(coll, bson, 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++; } Common.Disconnect(sdb); }
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(); }