public static void Main(string[] args) { var jb = new EJDB("zoo", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC); jb.ThrowExceptionOnFail = true; var parrot1 = BSONDocument.ValueOf(new { name = "Grenny", type = "African Grey", male = true, age = 1, birthdate = DateTime.Now, likes = new string[] { "green color", "night", "toys" }, extra = BSONull.VALUE }); var parrot2 = BSONDocument.ValueOf(new { name = "Bounty", type = "Cockatoo", male = false, age = 15, birthdate = DateTime.Now, likes = new string[] { "sugar cane" } }); jb.Save("parrots", parrot1, parrot2); Console.WriteLine("Grenny OID: " + parrot1["_id"]); Console.WriteLine("Bounty OID: " + parrot2["_id"]); var q = jb.CreateQuery(new { likes = "toys" }, "parrots").OrderBy("name"); using (var cur = q.Find()) { Console.WriteLine("Found " + cur.Length + " parrots"); foreach (var e in cur) { //fetch the `name` and the first element of likes array from the current BSON iterator. //alternatively you can fetch whole document from the iterator: `e.ToBSONDocument()` BSONDocument rdoc = e.ToBSONDocument("name", "likes.0"); Console.WriteLine(string.Format("{0} likes the '{1}'", rdoc["name"], rdoc["likes.0"])); } } q.Dispose(); jb.Dispose(); Console.ReadKey(); }
public void Test4Q1() { EJDB jb = new EJDB("testdb1", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC); Assert.IsTrue(jb.IsOpen); BSONDocument doc = new BSONDocument().SetNumber("age", 33); Assert.IsNull(doc["_id"]); bool rv = jb.Save("mycoll", doc); Assert.IsTrue(rv); Assert.IsNotNull(doc["_id"]); EJDBQuery q = jb.CreateQuery(BSONDocument.ValueOf(new{ age = 33 }), "mycoll"); Assert.IsNotNull(q); using (EJDBQCursor cursor = q.Find()) { Assert.IsNotNull(cursor); Assert.AreEqual(1, cursor.Length); int c = 0; foreach (BSONIterator oit in cursor) { c++; Assert.IsNotNull(oit); BSONDocument rdoc = oit.ToBSONDocument(); Assert.IsTrue(rdoc.HasKey("_id")); Assert.AreEqual(33, rdoc["age"]); } Assert.AreEqual(1, c); } using (EJDBQCursor cursor = q.Find(null, EJDBQuery.EXPLAIN_FLAG)) { Assert.IsNotNull(cursor); Assert.AreEqual(1, cursor.Length); Assert.IsTrue(cursor.Log.IndexOf("MAX: 4294967295") != -1); Assert.IsTrue(cursor.Log.IndexOf("SKIP: 0") != -1); Assert.IsTrue(cursor.Log.IndexOf("RS SIZE: 1") != -1); } q.Max(10); using (EJDBQCursor cursor = q.Find(null, EJDBQuery.EXPLAIN_FLAG)) { Assert.IsTrue(cursor.Log.IndexOf("MAX: 10") != -1); } q.Dispose(); jb.Dispose(); }
static void Main() { var httpWReq = (HttpWebRequest)WebRequest.Create("http://www.slamby.com/test/json/get/basic.json"); var response = (HttpWebResponse)httpWReq.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { var jsonContent = new StreamReader(stream: response.GetResponseStream()).ReadToEnd(); #region WithJSON2BSON var dbtest = new EJDB("dbb") { ThrowExceptionOnFail = true }; dbtest.Save("doctest", dbtest.Json2Bson(jsonContent)); var queryTest = dbtest.CreateQuery(new { sex = "male" }, "doctest"); using (var cursor = queryTest.Find()) { Console.WriteLine("Found" + cursor.Length + "names"); } #endregion var dictionary = JSON.Instance.Parse(jsonContent) as Dictionary <string, object>; if (dictionary != null) { var dict = new Dictionary <string, object>(dictionary); var db = new EJDB("db") { ThrowExceptionOnFail = true }; var bsonDocument = new BSONDocument(); foreach (var keyValuePair in dict) { if (keyValuePair.Value.GetType() != dict["dogs"].GetType()) { bsonDocument.SetString(keyValuePair.Key, keyValuePair.Value.ToString()); } else { if (dict["dogs"] as IEnumerable == null) { continue; } var dogsIndex = 0; var dogsBsonArray = new BSONArray(); foreach (var dog in dict["dogs"] as IEnumerable) { var dogsAttributes = JSON.Instance.Parse(dog as string) as Dictionary <string, string>; if (dogsAttributes != null) { var dogBsonValue = new BSONDocument(); foreach (var dogAttribute in dogsAttributes) { dogBsonValue.SetString(dogAttribute.Key, dogAttribute.Value); } dogsBsonArray.SetObject(dogsIndex, dogBsonValue); dogsIndex++; } } bsonDocument.SetArray("dogs", dogsBsonArray); } } db.Save("doc", bsonDocument); var query = db.CreateQuery(new { sex = "male" }, "doc"); /*using (var cursor = query.Find()) * { * Console.WriteLine("Found" + cursor.Length + "names"); * }*/ Console.ReadKey(); query.Dispose(); db.Dispose(); } } }
public void Test4Q2() { EJDB jb = new EJDB("testdb1", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC); Assert.IsTrue(jb.IsOpen); var parrot1 = BSONDocument.ValueOf(new{ name = "Grenny", type = "African Grey", male = true, age = 1, birthdate = DateTime.Now, likes = new string[] { "green color", "night", "toys" }, extra1 = BSONull.VALUE }); var parrot2 = BSONDocument.ValueOf(new{ name = "Bounty", type = "Cockatoo", male = false, age = 15, birthdate = DateTime.Now, likes = new string[] { "sugar cane" }, extra1 = BSONull.VALUE }); Assert.IsTrue(jb.Save("parrots", parrot1, parrot2)); Assert.AreEqual(2, jb.CreateQueryFor("parrots").Count()); var q = jb.CreateQuery(new{ name = new BSONRegexp("(grenny|bounty)", "i") }).SetDefaultCollection("parrots").OrderBy("name"); using (var cur = q.Find()) { Assert.AreEqual(2, cur.Length); var doc = cur[0].ToBSONDocument(); Assert.AreEqual("Bounty", doc["name"]); Assert.AreEqual(15, doc["age"]); doc = cur[1].ToBSONDocument(); Assert.AreEqual("Grenny", doc["name"]); Assert.AreEqual(1, doc["age"]); } q.Dispose(); q = jb.CreateQueryFor("parrots"); Assert.AreEqual(2, q.Count()); q.AddOR(new{ name = "Grenny" }); Assert.AreEqual(1, q.Count()); q.AddOR(new{ name = "Bounty" }); Assert.AreEqual(2, q.Count()); q.AddOR(new{ name = "Bounty2" }); Assert.AreEqual(2, q.Count()); //Console.WriteLine(jb.DBMeta); //[BSONDocument: [BSONValue: BSONType=STRING, Key=file, Value=testdb1], //[BSONValue: BSONType=ARRAY, Key=collections, Value=[BSONArray: [BSONValue: BSONType=OBJECT, Key=0, Value=[BSONDocument: [BSONValue: BSONType=STRING, Key=name, Value=parrots], [BSONValue: BSONType=STRING, Key=file, Value=testdb1_parrots], [BSONValue: BSONType=LONG, Key=records, Value=2], [BSONValue: BSONType=OBJECT, Key=options, Value=[BSONDocument: [BSONValue: BSONType=LONG, Key=buckets, Value=131071], [BSONValue: BSONType=LONG, Key=cachedrecords, Value=0], [BSONValue: BSONType=BOOL, Key=large, Value=False], [BSONValue: BSONType=BOOL, Key=compressed, Value=False]]], [BSONValue: BSONType=ARRAY, Key=indexes, Value=[BSONArray: ]]]]]]] q.Dispose(); //Test command execution BSONDocument cmret = jb.Command(BSONDocument.ValueOf(new { ping = "" })); Assert.IsNotNull(cmret); Assert.AreEqual("pong", cmret["log"]); jb.Dispose(); }