mongodb的增删改查基本操作

一、数据库操作

show dbs(show databases)

use database    //use or create  

db.dropDatabase()       

db.stats() //database information

 二、集合操作

(1)创建集合(指定创建)

db.createCollection(“collection_name1”)     or
db.createCollection("cn",{"capped":true,"size":1000,"max":100})

(2)创建集合(默认创建):向不存在的集合里插入文档

db.collection_name2.insert({“name”:”zs”,”age”:20})

(3)查看集合

 show tables 或 show collections

       db.collections.stats()

(4)删除集合

 db.collections.drop()

(5) 获取所有集合

db.getCollectionNames()

 三、文档操作

1.插入操作:

db.collections.insert({“name”:”zhangsan”,”age”:20})           

db.collections.insertOne({“name”:”wangwu”,”age”:18})
db.collections.insert([{},{},{}])

db.collections.insertMany([{},{},{}])
v=({“name”:”xiaobai”,”age”:”5”})


db.collections.insert(v)

 2.查询操作:

(1)无条件查询集合中文档

db.collections.find() 

 (2)带条件查询集合中文档

db.collections.find({})  

db.collections.find({“name”:”zhangsan”})

db.collections.find({“age”:30})

 (3)查询一个文档

db.collections.findOne()

db.collections.findOne({})

db.collections.findOne({“age”:20}) 

 (4)投影查询

db.collections.find({},{“name”:1}) //“_id”始终会被获取

db.collections.find({},{“name”:1,”_id”:0})//”_id”不会被获取

 (5)复合查询(或者,并且)

db.collections.find({“name”:”zhangsan”,“age”:25})         

db.collections.find({“$or”:[{“name”:”zhangsan”},{“age”:25}]})            

db.collections.find({“$or”:[{“name”:”zhangsan”},{“age”:25}],”sex”:1})

 (6)type查询

db.collections.find({“name”:{“$type”:”string”}})

(7)正则匹配

db.collections.find({“name”:/^z/})  //以z开头
或db.collections.find({“name”:{$regex:/^z/}}) 
   
db.collections.find({“name”:/y$/})  //以y结尾

db.collections.find({“name”:/^z.*y$/}) //一个点代表一个字符

db.collections.find({“name”:/z/}) //包含z

db.collections.find({“name”:/z/i}) //忽略大小写
或db.collections.find({“name”:{$regex:/^z/,$options:’i’}}) 

db.collections.find({“name”:{“$in”:[/^l/,/^z/]}})  $nin

db.collections.find({“name”:{$not:/^z/}})

(8)分页查询

  使用函数limit()和skip()来实现分页查询

 db.collections.find().limit().skip() //跳过多少行查询多少行

 (9)查询排序

 db.collectins.find().sort({“age”:-1,”name”:1})

 四、更新操作

1.覆盖更新:

Db.collections.update({“name”:”zhang”},{“name”:”li”,”age”:15})  

 //只会更新一条数据,按照集合的自然顺序

 2.表达式更新:

Db.collections.update({“name”:”zhang”},{“$set”:{“name”:”li”}})

//set会判断set的字段存在则修改,不存在则添加  

 五、删除操作

db.collections.remove({“age”:30})

 

db.collections.deleteOne({“age”:20})

db.collections.deleteMany({“age”:20})  

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用 C# 连接 MongoDB 进行增删的示例代码: 1. 安装 MongoDB 驱动程序 使用 NuGet 安装 MongoDB 驱动程序,可以在 Visual Studio 中搜索并安装 "MongoDB.Driver" 包。 2. 建立数据库连接 首先,您需要建立与 MongoDB 的连接。在 C# 中,可以使用 `MongoClient` 类来建立连接。 ```csharp using MongoDB.Driver; // 建立连接 var client = new MongoClient("mongodb://localhost:27017"); // 获取数据库 var database = client.GetDatabase("your_database_name"); ``` 3. 插入数据 使用 `InsertOne` 方法向 MongoDB 插入一条文档。 ```csharp using MongoDB.Bson; using MongoDB.Driver; // 获取集合 var collection = database.GetCollection<BsonDocument>("your_collection_name"); // 创建文档 var document = new BsonDocument { { "name", "John Doe" }, { "age", 30 }, { "city", "New York" } }; // 插入文档 collection.InsertOne(document); ``` 4. 询数据 使用 `Find` 方法从 MongoDB询数据。 ```csharp using MongoDB.Bson; using MongoDB.Driver; // 获取集合 var collection = database.GetCollection<BsonDocument>("your_collection_name"); // 询文档 var filter = Builders<BsonDocument>.Filter.Eq("name", "John Doe"); var result = collection.Find(filter).ToList(); // 打印结果 foreach (var document in result) { Console.WriteLine(document); } ``` 5. 更新数据 使用 `UpdateOne` 方法更新 MongoDB 中的数据。 ```csharp using MongoDB.Bson; using MongoDB.Driver; // 获取集合 var collection = database.GetCollection<BsonDocument>("your_collection_name"); // 更新文档 var filter = Builders<BsonDocument>.Filter.Eq("name", "John Doe"); var update = Builders<BsonDocument>.Update.Set("age", 35); collection.UpdateOne(filter, update); ``` 6. 删除数据 使用 `DeleteOne` 方法从 MongoDB 中删除数据。 ```csharp using MongoDB.Bson; using MongoDB.Driver; // 获取集合 var collection = database.GetCollection<BsonDocument>("your_collection_name"); // 删除文档 var filter = Builders<BsonDocument>.Filter.Eq("name", "John Doe"); collection.DeleteOne(filter); ``` 以上代码是基本增删操作示例,您可以根据具体需求进行扩展和修。请注意替换示例代码中的数据库名称和集合名称为您实际使用的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力代码不掉头发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值