public void TestIterate2() { var doc = new BsonDocument(); doc["a"] = "av"; doc["b"] = BsonDocument.ValueOf(new { cc = 1 }); var bsonOid = new ObjectId("51b9f3af98195c4600000000"); Console.WriteLine(bsonOid); doc["d"] = bsonOid; Assert.AreEqual(3, doc.KeysCount); //Console.WriteLine(doc.KeysCount); //Console.WriteLine(doc.ToDebugDataString()); //2E-00-00-00 +4 //02-61-00-03-00-00-00-61-76-00 +10 (14) //03-62-00 +3 (17) "d" = //0D-00-00-00 +4 (21) doc len = 13 //10-63-63-00-01-00-00-00 -00 +9 (30) //07-64-00 +3 (33) //51-B9-F3-AF-98-19-5C-46-00-00-00-00 +12 (45) //00 +1 (46) Assert.AreEqual("2E-00-00-00-" + "02-61-00-03-00-00-00-61-76-00-" + "03-62-00-" + "0D-00-00-00-" + "10-63-63-00-01-00-00-00-00-" + "07-64-00-" + "51-B9-F3-AF-98-19-5C-46-00-00-00-00-" + "00", doc.ToDebugDataString()); BsonIterator it = new BsonIterator(doc); int c = 0; foreach (var bt in it) { if (c == 0) { Assert.IsTrue(bt == BsonType.STRING); } if (c == 1) { Assert.IsTrue(bt == BsonType.OBJECT); } if (c == 2) { Assert.IsTrue(bt == BsonType.OID); } ++c; } bool thrown = false; Assert.IsTrue(it.Disposed); try { it.Next(); } catch (ObjectDisposedException) { thrown = true; } Assert.IsTrue(thrown); c = 0; it = new BsonIterator(doc); foreach (var bv in it.Values()) { if (c == 0) { Assert.AreEqual("a", bv.Key); Assert.AreEqual("av", bv.Value); } if (c == 1) { Assert.AreEqual("b", bv.Key); BsonDocument sdoc = bv.Value as BsonDocument; Assert.IsNotNull(sdoc); foreach (var bv2 in new BsonIterator(sdoc).Values()) { Assert.AreEqual("cc", bv2.Key); Assert.AreEqual(1, bv2.Value); Assert.AreEqual(BsonType.INT, bv2.BsonType); } } if (c == 2) { Assert.AreEqual(BsonType.OID, bv.BsonType); Assert.IsInstanceOf(typeof(ObjectId), bv.Value); var oid = bv.Value is ObjectId ? (ObjectId)bv.Value : new ObjectId(); Assert.AreEqual("51b9f3af98195c4600000000", oid.ToString()); } c++; } }