public void TestFilteredDoc() { var doc = new BSONDocument(); doc["c"] = "d"; doc["aaa"] = 11; doc["ndoc"] = BSONDocument.ValueOf(new { aaaa = "nv1", d = "nv2", nnd = BSONDocument.ValueOf(new { nnv = true, nns = "s" }) }); doc["ndoc2"] = BSONDocument.ValueOf(new { n = "v" }); doc["f"] = "f"; BSONIterator it = new BSONIterator(doc); BSONDocument doc2 = it.ToBSONDocument("c", "ndoc.d", "ndoc.nnd.nns", "f"); Assert.AreEqual(3, doc2.KeysCount); Assert.AreEqual("d", doc2["c"]); Assert.AreEqual(2, ((BSONDocument) doc2["ndoc"]).KeysCount); Assert.AreEqual("nv2", ((BSONDocument) doc2["ndoc"])["d"]); Assert.AreEqual("s", ((BSONDocument) ((BSONDocument) doc2["ndoc"])["nnd"])["nns"]); Assert.AreEqual("s", doc2["ndoc.nnd.nns"]); Assert.AreEqual("f", "f"); //Console.WriteLine("doc2=" + doc2); }
public void TestFilteredDoc() { var doc = new BSONDocument(); doc["c"] = "d"; doc["aaa"] = 11; doc["ndoc"] = BSONDocument.ValueOf(new { aaaa = "nv1", d = "nv2", nnd = BSONDocument.ValueOf(new { nnv = true, nns = "s" }) }); doc["ndoc2"] = BSONDocument.ValueOf(new { n = "v" }); doc["f"] = "f"; BSONIterator it = new BSONIterator(doc); BSONDocument doc2 = it.ToBSONDocument("c", "ndoc.d", "ndoc.nnd.nns", "f"); Assert.AreEqual(3, doc2.KeysCount); Assert.AreEqual("d", doc2["c"]); Assert.AreEqual(2, ((BSONDocument)doc2["ndoc"]).KeysCount); Assert.AreEqual("nv2", ((BSONDocument)doc2["ndoc"])["d"]); Assert.AreEqual("s", ((BSONDocument)((BSONDocument)doc2["ndoc"])["nnd"])["nns"]); Assert.AreEqual("s", doc2["ndoc.nnd.nns"]); Assert.AreEqual("f", "f"); //Console.WriteLine("doc2=" + doc2); }
/// <summary> /// Convert JSON string into BSONDocument. /// Returns `null` if conversion failed. /// </summary> /// <returns>The BSONDocument instance on success.</returns> /// <param name="json">JSON string</param> public BSONDocument Json2Bson(string json) { IntPtr bsonret = _json2bson(json); if (bsonret == IntPtr.Zero) { return(null); } byte[] bsdata = BsonPtrIntoByteArray(bsonret); if (bsdata.Length == 0) { return(null); } BSONIterator it = new BSONIterator(bsdata); return(it.ToBSONDocument()); }
/// <summary> /// Executes EJDB command. /// </summary> /// <remarks> /// Supported commands: /// /// 1) Exports database collections data. See ejdbexport() method. /// /// "export" : { /// "path" : string, //Exports database collections data /// "cnames" : [string array]|null, //List of collection names to export /// "mode" : int|null //Values: null|`JBJSONEXPORT` See ejdb.h#ejdbexport() method /// } /// /// Command response: /// { /// "log" : string, //Diagnostic log about executing this command /// "error" : string|null, //ejdb error message /// "errorCode" : int|0, //ejdb error code /// } /// /// 2) Imports previously exported collections data into ejdb. /// /// "import" : { /// "path" : string //The directory path in which data resides /// "cnames" : [string array]|null, //List of collection names to import /// "mode" : int|null //Values: null|`JBIMPORTUPDATE`|`JBIMPORTREPLACE` See ejdb.h#ejdbimport() method /// } /// /// Command response: /// { /// "log" : string, //Diagnostic log about executing this command /// "error" : string|null, //ejdb error message /// "errorCode" : int|0, //ejdb error code /// } /// </remarks> /// <param name="cmd">Command object</param> /// <returns>Command response.</returns> public BSONDocument Command(BSONDocument cmd) { CheckDisposed(); byte[] cmdata = cmd.ToByteArray(); //internal static extern IntPtr _ejdbcommand([In] IntPtr db, [In] byte[] cmd); IntPtr cmdret = _ejdbcommand(_db, cmdata); if (cmdret == IntPtr.Zero) { return(null); } byte[] bsdata = BsonPtrIntoByteArray(cmdret); if (bsdata.Length == 0) { return(null); } BSONIterator it = new BSONIterator(bsdata); return(it.ToBSONDocument()); }
public void Test3SaveLoad() { 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"]); Assert.IsInstanceOf(typeof(BSONOid), doc["_id"]); rv = jb.Save("mycoll", doc); Assert.IsTrue(rv); BSONIterator it = jb.Load("mycoll", doc["_id"] as BSONOid); Assert.IsNotNull(it); BSONDocument doc2 = it.ToBSONDocument(); Assert.AreEqual(doc.ToDebugDataString(), doc2.ToDebugDataString()); Assert.IsTrue(doc == doc2); Assert.AreEqual(1, jb.CreateQueryFor("mycoll").Count()); Assert.IsTrue(jb.Remove("mycoll", doc["_id"] as BSONOid)); Assert.AreEqual(0, jb.CreateQueryFor("mycoll").Count()); jb.Save("mycoll", doc); Assert.AreEqual(1, jb.CreateQueryFor("mycoll").Count()); Assert.IsTrue(jb.DropCollection("mycoll")); Assert.AreEqual(0, jb.CreateQueryFor("mycoll").Count()); Assert.IsTrue(jb.Sync()); jb.Dispose(); }
/// <summary> /// Executes EJDB command. /// </summary> /// <remarks> /// Supported commands: /// /// 1) Exports database collections data. See ejdbexport() method. /// /// "export" : { /// "path" : string, //Exports database collections data /// "cnames" : [string array]|null, //List of collection names to export /// "mode" : int|null //Values: null|`JBJSONEXPORT` See ejdb.h#ejdbexport() method /// } /// /// Command response: /// { /// "log" : string, //Diagnostic log about executing this command /// "error" : string|null, //ejdb error message /// "errorCode" : int|0, //ejdb error code /// } /// /// 2) Imports previously exported collections data into ejdb. /// /// "import" : { /// "path" : string //The directory path in which data resides /// "cnames" : [string array]|null, //List of collection names to import /// "mode" : int|null //Values: null|`JBIMPORTUPDATE`|`JBIMPORTREPLACE` See ejdb.h#ejdbimport() method /// } /// /// Command response: /// { /// "log" : string, //Diagnostic log about executing this command /// "error" : string|null, //ejdb error message /// "errorCode" : int|0, //ejdb error code /// } /// </remarks> /// <param name="cmd">Command object</param> /// <returns>Command response.</returns> public BSONDocument Command(BSONDocument cmd) { CheckDisposed(); byte[] cmdata = cmd.ToByteArray(); //internal static extern IntPtr _ejdbcommand([In] IntPtr db, [In] byte[] cmd); IntPtr cmdret = _ejdbcommand(_db, cmdata); if (cmdret == IntPtr.Zero) { return null; } byte[] bsdata = BsonPtrIntoByteArray(cmdret); if (bsdata.Length == 0) { return null; } BSONIterator it = new BSONIterator(bsdata); return it.ToBSONDocument(); }
/// <summary> /// Convert JSON string into BSONDocument. /// Returns `null` if conversion failed. /// </summary> /// <returns>The BSONDocument instance on success.</returns> /// <param name="json">JSON string</param> public BSONDocument Json2Bson(string json) { IntPtr bsonret = _json2bson(json); if (bsonret == IntPtr.Zero) { return null; } byte[] bsdata = BsonPtrIntoByteArray(bsonret); if (bsdata.Length == 0) { return null; } BSONIterator it = new BSONIterator(bsdata); return it.ToBSONDocument(); }