コード例 #1
0
ファイル: Program.cs プロジェクト: jfonseca87/MongoWithCSharp
        static void CreateMongoWithBsonDocument()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            //Save Data In Mongo using a BsonDocument object
            var document = new BsonDocument
            {
                { "name", "Winston Churchill" },
                { "undergrad", true },
                { "units", "3" },
                { "classes", new BsonArray {
                      "English",
                      "Math",
                      "Spanish"
                  } },
            };

            collection.InsertOne(document);

            Console.WriteLine("The record has been successfully created (scenario 1)");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: jfonseca87/MongoWithCSharp
        static void CreateMongoWithClass()
        {
            var collection = new MongoDb <Student>().GetCollection("student");

            //Save Data In Mongo using a BsonDocument object
            var document = new Student
            {
                Id        = ObjectId.GenerateNewId(),
                Name      = "Keanu Reeves",
                Undergrad = false,
                Units     = 3,
                Classes   = new List <string>
                {
                    "History",
                    "Math",
                    "Chemistry"
                }
            };

            collection.InsertOne(document);

            Console.WriteLine("The record has been successfully created (scenario 2)");
        }