• <七>.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; }
    }

  • 相关阅读:
    RabbitMQ知识点整理2-相关概念介绍
    RabbitMQ知识点整理1-生产和消费消息
    Java自学-图形界面 Swing中的线程
    Java自学-图形界面 日期控件
    Java自学-图形界面 表格
    Java自学-图形界面 工具栏
    WebStorm 2020.2.3 破解(Mac,windows,Linux)
    消息队列的使用场景
    有关建立虚拟环境的总结
    性能测试-Locust分布式执行
  • 原文地址:https://www.cnblogs.com/choii/p/16384077.html
Copyright © 2020-2023  润新知