Пример #1
0
        static void Poco(string[] args)
        {
            var conventionPack = new ConventionPack();
            conventionPack.Add(new CamelCaseElementNameConvention());
            ConventionRegistry.Register("camelCase", conventionPack, t => true);

            BsonClassMap.RegisterClassMap<Person>(cm =>
            {
                cm.AutoMap();
                cm.MapMember(x => x.Name).SetElementName("name");
            });

            var person = new Person
            {
                Name = "Jones",
                Age = 30,
                Colors = new List<string> { "red", "blue" },
                Pets = new List<Pet> { new Pet { Name = "Fluffy", Type = "Pig" } },
                ExtraElements = new BsonDocument("anotherName", "anotherValue")
            };

            using (var writer = new JsonWriter(Console.Out))
            {
                BsonSerializer.Serialize(writer, person);
            }
        }
Пример #2
0
        static async Task InsertOneAndMany(string[] args)
        {
            var client = new MongoClient();
            var db = client.GetDatabase("test");

            var col = db.GetCollection<BsonDocument>("people");
            var peopleCollection = db.GetCollection<Person>("people");

            var doc = new BsonDocument
            {
                { "Name", "Emily" },
                { "Age", 25 },
                { "Profession", "Designer" }
            };

            var doc2 = new BsonDocument
            {
                { "Name", "Georges" },
                { "Age", 28 },
                { "Profession", "Doctor" }
            };

            var person = new Person
            {
                Name = "Martin",
                Age = 49,
                Profession = "Hacker"
            };

            await col.InsertOneAsync(doc2);
            doc2.Remove("_id");
            await col.InsertOneAsync(doc2);

            //await col.InsertManyAsync(new[] { doc, doc2 });
            //await peopleCollection.InsertOneAsync(person);
        }