• <七>.netcore更新删除MongoDB


    • UpdateMany 批量更新,UpdateOne更新一个
    var connectionString = "mongodb://localhost:27017";
    var client = new MongoClient(connectionString);
    var database = client.GetDatabase("School");
    var collection = database.GetCollection<StudentNew>("StudentNew");
    var filter = Builders<StudentNew>.Filter.Where(m => m.Sex =="");
    var update = Builders<StudentNew>.Update.Set(m => m.Name, "隔壁老王");
    collection.UpdateMany(filter, update); 
    await collection.Find(x => x.Sex =="")
        .Sort("{Age:1}")
        .Project("{Name:1}")
        .ForEachAsync(student =>
        {
            Console.WriteLine(student);
        });
    class StudentNew
    {
        public ObjectId _id { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
        public IEnumerable<string> Address { get; set; }
        public string Sex { get; set; }
    }

    • 批量删除DeleteMany,单个删除DeleteOne
    var connectionString = "mongodb://localhost:27017";
    var client = new MongoClient(connectionString);
    var database = client.GetDatabase("School");
    var collection = database.GetCollection<StudentNew>("StudentNew");
    var filter = Builders<StudentNew>.Filter.Where(m => m.Sex =="");
    collection.DeleteMany(filter);
    //collection.DeleteOne(filter);
    await collection.Find(FilterDefinition<StudentNew>.Empty)
        .Sort("{Age:1}")
        .Project("{Name:1}")
        .ForEachAsync(student =>
        {
            Console.WriteLine(student);
        });
    class StudentNew
    {
        public ObjectId _id { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
        public IEnumerable<string> Address { get; set; }
        public string Sex { get; set; }
    }

  • 相关阅读:
    21. Merge Two Sorted Lists
    496. Next Greater Element I
    (转载)深度学习的weight initialization
    Python collections模块
    Iterables vs. Iterators vs. Generators
    (转)iPhone开发关于UDID和UUID的一些理解
    uniqueIdentifier在ios7不支持后的替代方法
    Android——列表视图 ListView(一)Arrayadapter
    Android——对话框2(日期和时间对话框)
    Android——子线程操作主线程
  • 原文地址:https://www.cnblogs.com/choii/p/16384077.html
Copyright © 2020-2023  润新知