Exemplo n.º 1
0
        static void FilterUsinEQWithString()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            var filter = "{undergrad: {'$eq': false}}";

            collection.Find(filter).ForEachAsync(doc => Console.WriteLine(doc));
        }
Exemplo n.º 2
0
        static void FilterUsinEQWithFilterDifinitionBuilder()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            var filter = new FilterDefinitionBuilder <BsonDocument>().Eq("undergrad", false);

            collection.Find(filter).ForEachAsync(doc => Console.WriteLine(doc));
        }
Exemplo n.º 3
0
        static void FilterWithString()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            var filter = "{Name: 'Keanu Reeves'}";

            collection.Find(filter).ForEachAsync(doc => Console.WriteLine(doc));
        }
Exemplo n.º 4
0
        static void FilterUsinEQWithBsonDocument()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            var filter = new BsonDocument("undergrad", new BsonDocument("$eq", false));

            collection.Find(filter).ForEachAsync(doc => Console.WriteLine(doc));
        }
Exemplo n.º 5
0
        static void FilterWithBsonDocument()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            var filter = new BsonDocument("Name", "Keanu Reeves");

            collection.Find(filter).ForEachAsync(doc => Console.WriteLine(doc));
        }
Exemplo n.º 6
0
        static void DeleteWithBsonDocument()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            var filter = new BsonDocument("_id", new ObjectId("5ea24b49dc6da638a88216d7"));

            collection.DeleteOne(filter);

            Console.WriteLine("The record has been succcessfully deleted");
        }
Exemplo n.º 7
0
        static void FilterByIdWithString()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            var filter = "{_id: ObjectId('5ea37c1edc6da61880bff131')}";

            var doc = collection.Find(filter).FirstOrDefault();

            Console.WriteLine(doc);
        }
Exemplo n.º 8
0
        static void FilterByIdWithBsonDocument()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            var filter = new BsonDocument("_id", new ObjectId("5ea37c1edc6da61880bff131"));

            var doc = collection.Find(filter).FirstOrDefault();

            Console.WriteLine(doc);
        }
Exemplo n.º 9
0
        static void CreateMongoManyWithClass()
        {
            var collection = new MongoDb <Student>().GetCollection("student");

            var documents = new List <Student>
            {
                new Student
                {
                    Id        = ObjectId.GenerateNewId(),
                    Name      = "Keanu Reeves",
                    Undergrad = false,
                    Units     = 3,
                    Classes   = new List <string>
                    {
                        "History",
                        "Math",
                        "Chemistry"
                    }
                },
                new Student
                {
                    Id        = ObjectId.GenerateNewId(),
                    Name      = "Keanu Reeves",
                    Undergrad = false,
                    Units     = 3,
                    Classes   = new List <string>
                    {
                        "History",
                        "Math",
                        "Chemistry"
                    }
                },
                new Student
                {
                    Id        = ObjectId.GenerateNewId(),
                    Name      = "Keanu Reeves",
                    Undergrad = false,
                    Units     = 3,
                    Classes   = new List <string>
                    {
                        "History",
                        "Math",
                        "Chemistry"
                    }
                }
            };

            collection.InsertMany(documents);

            Console.WriteLine("The records has been successfully created (scenario 4)");
        }
Exemplo n.º 10
0
        static void CreateMongoManyWithBsonDocument()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            var documents = new List <BsonDocument>
            {
                new BsonDocument
                {
                    { "name", "Winston Churchill" },
                    { "undergrad", true },
                    { "units", "3" },
                    { "classes", new BsonArray {
                          "English",
                          "Math",
                          "Spanish"
                      } }
                },
                new BsonDocument
                {
                    { "name", "Winston Churchill" },
                    { "undergrad", true },
                    { "units", "3" },
                    { "classes", new BsonArray {
                          "English",
                          "Math",
                          "Spanish"
                      } }
                },
                new BsonDocument
                {
                    { "name", "Winston Churchill" },
                    { "undergrad", true },
                    { "units", "3" },
                    { "classes", new BsonArray {
                          "English",
                          "Math",
                          "Spanish"
                      } }
                },
            };

            collection.InsertMany(documents);

            Console.WriteLine("The records has been successfully created (scenario 3)");
        }
Exemplo n.º 11
0
        static void UpdateWithBsonDocument()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            var filter       = new BsonDocument("_id", new ObjectId("5ea24b49dc6da638a88216d7"));
            var updateValues = new BsonDocument(new List <BsonElement>
            {
                new BsonElement("name", "Aurora Boreal"),
                new BsonElement("undergrad", true),
                new BsonElement("units", 10),
                new BsonElement("classes", new BsonArray {
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                })
            });

            collection.ReplaceOne(filter, updateValues);

            Console.WriteLine("The record has been succcessfully updated");
        }
Exemplo n.º 12
0
        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)");
        }
Exemplo n.º 13
0
        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)");
        }
Exemplo n.º 14
0
        static void FilterUsinEQWithLambdaExpression()
        {
            var collection = new MongoDb <Student>().GetCollection("student");

            collection.Find(x => x.Undergrad == false).ForEachAsync(doc => Console.WriteLine($"{doc.Id} - {doc.Name}"));
        }
Exemplo n.º 15
0
        static void UseOfFind()
        {
            var collection = new MongoDb <BsonDocument>().GetCollection("student");

            collection.Find(FilterDefinition <BsonDocument> .Empty).ForEachAsync(doc => Console.WriteLine(doc));
        }