首页 > 学院 > 开发设计 > 正文

MongoDB 常用操作

2019-11-08 20:48:53
字体:
来源:转载
供稿:网友
show dbs:显示数据库列表 show collections:显示当前数据库中的集合(类似关系数据库中的表) show users:显示用户

Database

# 切换到指定数据库,如果没有新建,否则切换到指定的库,切换后,如果没有任何操作,是看不到库的,可以尝试插入一条数据。use dbnamedb.test.insert({"name":"mongodb"})show dbs# 删除库db.dropDatabase()

Collection

show collectionsdb.createCollection("mycollection")db.createCollection("mycol", {capped:true,autoIndexID:true,size:6142800,max:10000})db.mycollection.drop()

CRUD

# insertdb.test.insert({"name":"mongodb"})db.test.insert({"name":"mongodb3.4"})# removedb.test.remove({"name":"mongodb"})# updatedb.test.update({"name":"mongodb3.4"}, {$set:{"name":"MongoDB3.4"}})# finddb.test.find()db.test.find().PRetty()# 等于db.test.find({"name":"MongoDB"})# 小于 age < 50db.test.find({"age":{$lt:50}})# 小于或等于 age <= 50db.test.find({"age":{$lte:50}})# 大于 age > 50db.test.find({"age":{$gt:50}})# 大于或等于 age >= 50db.test.find({"age":{$gte:50}})# 不等于 age != 50db.test.find({"age":{$ne:50}})
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表