static async Task BsonDocMethod() { var doc = new BsonDocument { {"name", "Sanya" } }; doc.Add("age", 30); doc["profession"] = "hacker"; var nestedArray = new BsonArray(); nestedArray.Add(new BsonDocument("color", "red")); doc.Add("array", nestedArray); Console.WriteLine(1); //Console.WriteLine(doc.TryGetElement("name", out name)); Console.WriteLine(doc.Count()); Console.WriteLine(); Console.WriteLine(doc.ContainsValue("Sanya")); Console.WriteLine(doc.Contains("name")); Console.WriteLine(doc); //var doc2 = new BsonDocument //{ // { "Name", "Smith"}, // {"Something", "something else" }, // {"profession", "hacker" } //}; //await collectionUsed.InsertManyAsync(new[] { doc, doc2 }); }
static async Task BsonDocMethod() { var doc = new BsonDocument { { "name", "Sanya" } }; doc.Add("age", 30); doc["profession"] = "hacker"; var nestedArray = new BsonArray(); nestedArray.Add(new BsonDocument("color", "red")); doc.Add("array", nestedArray); Console.WriteLine(1); //Console.WriteLine(doc.TryGetElement("name", out name)); Console.WriteLine(doc.Count()); Console.WriteLine(); Console.WriteLine(doc.ContainsValue("Sanya")); Console.WriteLine(doc.Contains("name")); Console.WriteLine(doc); //var doc2 = new BsonDocument //{ // { "Name", "Smith"}, // {"Something", "something else" }, // {"profession", "hacker" } //}; //await collectionUsed.InsertManyAsync(new[] { doc, doc2 }); }
public void TestCreateFromDictionary() { var dictionary = new Dictionary<string, object> { { "x", 1 }, { "n", null }, { "a", new object[] { 1, null } } }; // null will be mapped to BsonNull.Value var document = new BsonDocument(dictionary); Assert.AreEqual(3, document.ElementCount); Assert.AreEqual(1, document["x"].AsInt32); Assert.AreSame(BsonNull.Value, document["n"]); Assert.IsTrue(document["a"].IsBsonArray); Assert.AreEqual(2, document["a"].AsBsonArray.Count); Assert.AreEqual(1, document["a"][0].AsInt32); Assert.AreSame(BsonNull.Value, document["a"][1]); Assert.AreEqual(true, document.Contains("x")); Assert.AreEqual(true, document.Contains("n")); Assert.AreEqual(true, document.Contains("a")); Assert.AreEqual(true, document.ContainsValue(1)); Assert.AreEqual(true, document.ContainsValue(BsonNull.Value)); }
public void TestContainsValue() { var document = new BsonDocument(); Assert.IsFalse(document.ContainsValue(1)); document["x"] = 1; Assert.IsTrue(document.ContainsValue(1)); }
public void TestConstructorNoArgs() { var document = new BsonDocument(); Assert.AreEqual(false, document.AllowDuplicateNames); Assert.IsInstanceOf<BsonDocument>(document.AsBsonDocument); Assert.AreEqual(BsonType.Document, document.BsonType); Assert.AreEqual(false, document.Contains("name")); Assert.AreEqual(false, document.ContainsValue(0)); Assert.AreEqual(0, document.ElementCount); Assert.AreEqual(0, document.Elements.Count()); Assert.AreEqual(false, document.IsBsonArray); Assert.AreEqual(true, document.IsBsonDocument); Assert.AreEqual(0, document.Names.Count()); #pragma warning disable 618 Assert.AreEqual(null, document.RawValue); Assert.AreEqual(0, document.RawValues.Count()); #pragma warning restore Assert.AreEqual(true, document.ToBoolean()); Assert.AreSame(document, document.ToBsonDocument()); Assert.AreEqual("{ }", document.ToJson()); Assert.AreEqual(0, document.Values.Count()); }
public void TestConstructorNameValue() { var document = new BsonDocument("x", 1); Assert.AreEqual(1, document.ElementCount); Assert.AreEqual(1, document["x"].AsInt32); Assert.AreEqual(true, document.Contains("x")); Assert.AreEqual(true, document.ContainsValue(1)); }
public void TestConstructorIDictionaryWithKeys() { var hashtable = (IDictionary)new Hashtable { { "x", 1 }, { "y", 2 } }; var keys = new string[] { "x" }; #pragma warning disable 618 var document = new BsonDocument(hashtable, keys); #pragma warning restore Assert.AreEqual(1, document.ElementCount); Assert.AreEqual(1, document["x"].AsInt32); Assert.AreEqual(true, document.Contains("x")); Assert.AreEqual(true, document.ContainsValue(1)); }
public void TestConstructorIDictionaryGenericWithKeys() { var dictionary = (IDictionary<string, object>)new Dictionary<string, object> { { "x", 1 }, { "y", 2 } }; var keys = new string[] { "x" }; #pragma warning disable 618 var document = new BsonDocument(dictionary, keys); #pragma warning restore Assert.AreEqual(1, document.ElementCount); Assert.AreEqual(1, document["x"].AsInt32); Assert.AreEqual(true, document.Contains("x")); Assert.AreEqual(true, document.ContainsValue(1)); }
public void TestConstructorIDictionaryGeneric() { var dictionary = (IDictionary<string, object>)new Dictionary<string, object> { { "x", 1 } }; var document = new BsonDocument(dictionary); Assert.AreEqual(1, document.ElementCount); Assert.AreEqual(1, document["x"].AsInt32); Assert.AreEqual(true, document.Contains("x")); Assert.AreEqual(true, document.ContainsValue(1)); }
public void TestConstructorIDictionary() { var hashtable = (IDictionary)new Hashtable { { "x", 1 } }; var document = new BsonDocument(hashtable); Assert.AreEqual(1, document.ElementCount); Assert.AreEqual(1, document["x"].AsInt32); Assert.AreEqual(true, document.Contains("x")); Assert.AreEqual(true, document.ContainsValue(1)); }
public void TestConstructorElementsParams() { var element1 = new BsonElement("x", 1); var element2 = new BsonElement("y", 2); #pragma warning disable 618 var document = new BsonDocument(element1, element2); #pragma warning restore Assert.AreEqual(2, document.ElementCount); Assert.AreEqual(1, document["x"].AsInt32); Assert.AreEqual(2, document["y"].AsInt32); Assert.AreEqual(true, document.Contains("x")); Assert.AreEqual(true, document.Contains("y")); Assert.AreEqual(true, document.ContainsValue(1)); Assert.AreEqual(true, document.ContainsValue(2)); Assert.AreSame(element1, document.GetElement("x")); Assert.AreSame(element2, document.GetElement("y")); }
public void TestConstructorElementsDocument() { var originalDocument = new BsonDocument { { "x", 1 }, { "y", 2 } }; var document = new BsonDocument(originalDocument); Assert.AreEqual(2, document.ElementCount); Assert.AreEqual(1, document["x"].AsInt32); Assert.AreEqual(2, document["y"].AsInt32); Assert.AreEqual(true, document.Contains("x")); Assert.AreEqual(true, document.Contains("y")); Assert.AreEqual(true, document.ContainsValue(1)); Assert.AreEqual(true, document.ContainsValue(2)); Assert.AreSame(originalDocument.GetElement("x"), document.GetElement("x")); Assert.AreSame(originalDocument.GetElement("y"), document.GetElement("y")); }
public void TestConstructorElements() { var elements = new BsonElement[] { new BsonElement("x", 1), new BsonElement("y", 2) }; var document = new BsonDocument((IEnumerable<BsonElement>)elements); Assert.AreEqual(2, document.ElementCount); Assert.AreEqual(1, document["x"].AsInt32); Assert.AreEqual(2, document["y"].AsInt32); Assert.AreEqual(true, document.Contains("x")); Assert.AreEqual(true, document.Contains("y")); Assert.AreEqual(true, document.ContainsValue(1)); Assert.AreEqual(true, document.ContainsValue(2)); Assert.AreSame(elements[0], document.GetElement("x")); Assert.AreSame(elements[1], document.GetElement("y")); }
public void TestConstructorElement() { var element = new BsonElement("x", 1); var document = new BsonDocument(element); Assert.Equal(1, document.ElementCount); Assert.Equal(1, document["x"].AsInt32); Assert.Equal(true, document.Contains("x")); Assert.Equal(true, document.ContainsValue(1)); Assert.Same(element.Value, document.GetElement("x").Value); }