MongoDB C# Driver usage example (2.2)

黄舟
Release: 2017-02-28 11:48:32
Original
1410 people have browsed it

The project has been updated to mongoDB C# driver 2.2. It is found that the changes from 1.9 to 2.0 are still very big. Some common operations have been integrated and additional demo code has been integrated:

  class Program
    {
        const string CollectionName = "video";
        static void Main(string[] args)
        {
            // remove the demo collection then recreate later
            db.GetCollection<Video>(CollectionName).Database.DropCollection(CollectionName);


            var videos = new List<Video>
            {
                new Video { Title="The Perfect Developer", 
                            Category="SciFi", Minutes=118 },
                new Video { Title="Lost In Frankfurt am Main", 
                            Category="Horror", Minutes=122 }, 
                new Video { Title="The Infinite Standup", 
                            Category="Horror", Minutes=341 } 
            };


            Console.WriteLine("Insert Videos ...");


            db.GetCollection<Video>(CollectionName).InsertMany(videos);


            Console.WriteLine("[After insert] All Videos : ");
            var all = db.GetCollection<Video>(CollectionName).Find(x=>x.Title != string.Empty).ToList();
            foreach (var v in all)
            {
                Console.WriteLine(v);
            }


            Console.WriteLine("Group By...");


            var groupby = db.GetCollection<Video>(CollectionName).Aggregate()
                    .Group(x => x.Category, g => new {Name = g.Key, Count = g.Count(), TotalMinutes = g.Sum(x => x.Minutes)})
                    .ToList();
            foreach (var v in groupby)
            {
                Console.WriteLine(v.Name + "," + v.Count + "," + v.TotalMinutes);
            }




            Console.WriteLine("Updating One [Title = The Perfect Developer]...");


            // updating title with "The perfect developer" video&#39;s &#39;title&#39; and &#39;minute&#39;
            db.GetCollection<Video>(CollectionName).FindOneAndUpdate(x=>x.Title == "The Perfect Developer",
                    Builders<Video>.Update.Set(x=> x.Title , "A Perfect Developer [updated]")
                                          .AddToSet(x => x.Comments, "good video!")
                                          .AddToSet(x => x.Comments, "not bad"));


            Console.WriteLine("[After Updating One] All Videos : ");
            all = db.GetCollection<Video>(CollectionName).Find(x => x.Title != string.Empty).ToList();
            foreach (var v in all)
            {
                Console.WriteLine(v);
            }


            Console.WriteLine("Deleting One... [Minutes = 122]");
            db.GetCollection<Video>(CollectionName).DeleteOne(x => x.Minutes == 122);
            Console.WriteLine("[After Deleting One] All Videos : ");
            all = db.GetCollection<Video>(CollectionName).Find(x => x.Title != string.Empty).ToList();
            foreach (var v in all)
            {
                Console.WriteLine(v);
            }


            Console.Read();
        }


        private static IMongoDatabase db
        {
            get
            {
                var url = new MongoUrl(ConfigurationSettings.AppSettings["mongoUrl"]);
                var client = new MongoClient(url);
                return client.GetDatabase(url.DatabaseName);
            }
        }
    }


    [BsonIgnoreExtraElements]
    public class Video
    {
        public Video()
        {
            Comments = new List<string>();
        }


        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }


        public string Title { get; set; }
        public string Category { get; set; }
        public int Minutes { get; set; }


        public IList<string> Comments { get; set; }


        public override string ToString()
        {
            return string.Format("{0} - {1} - {2}", Title, Category, Minutes);
        }
    }
Copy after login

The update of the mongoDB C# driver from 1.9 to 2.0 simplifies the database connection method, simplifies the interfaces of Find, Update, and Delete, and the group by and projection operations are also smoother.
In Builders, update, filter, projection, and sort are integrated, making the functions more cohesive and making it easy to find functions.

The above is the content of the MongoDB C# Driver usage example (2.2). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!