欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MongoDB的基本使用以及增删改查

程序员文章站 2022-11-17 18:47:02
mongodb的基本使用以及增删改查 一.配置环境 1.官网下载安装mongodb 2.配置环境变量 3.创建保存的文件夹 4.开启数据库服务端 mongod --dbp...

mongodb的基本使用以及增删改查

一.配置环境

1.官网下载安装mongodb 2.配置环境变量 3.创建保存的文件夹 4.开启数据库服务端

mongod --dbpath [file]

5.链接数据库

mongo //本地连接直接输入mongo
mongo  127.0.0.27017   //非本地链接   输入数据库的地址

二 .基本的一些操作

使用数据库 ,创建数据库

use [database_name]     //创建数据库 
use class    //创建了一个名字叫class的数据库 

如果想把这个数据库创建成功,就必须再在里面创建一个表(集合),集合里面添加一条数据

> use class
switched to db class
> db.students.insert({"name":"zhangsan","age":18,"sex":"男"})
writeresult({ "ninserted" : 1 })
//在students这个表里面添加了一条数据 
//只有添加了数据,这个class数据库才算是创建成功 

//多创建几条
> db.students.insert({"name":"wangwu","age":19,"sex":"男"})
writeresult({ "ninserted" : 1 })
> db.students.insert({"name":"xiaohong","age":18,"sex":"女"})
writeresult({ "ninserted" : 1 })
> db.students.insert({"name":"xiaolan","age":16,"sex":"女"}))
writeresult({ "ninserted" : 1 })
> db.students.insert({"name":"xiaohuang","age":17,"sex":"男"})
writeresult({ "ninserted" : 1 })
> db.students.insert({"name":"xiaohuang","age":17,"sex":"男"})
writeresult({ "ninserted" : 1 })
> db.students.insert({"name":"小黄","age":20,"sex":"男"}))))))
writeresult({ "ninserted" : 1 })
> db.students.insert({"name":"小红","age":21,"sex":"女"})
writeresult({ "ninserted" : 1 })
> db.students.insert({"name":"小兰","age":21,"sex":"女"})
writeresult({ "ninserted" : 1 })
//查询集合   db.[表名].find()
> db.students.find()    
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd70848a20424a081c010d3"), "name" : "wangwu", "age" : 19, "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd7086aa20424a081c010d5"), "name" : "xiaolan", "age" : 16, "sex" : "女" }
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }
{ "_id" : objectid("5bd708b4a20424a081c010d9"), "name" : "小红", "age" : 21, "sex" : "女" }
{ "_id" : objectid("5bd708c0a20424a081c010da"), "name" : "小兰", "age" : 21, "sex" : "女" }
//根据条件精准查找 
> db.students.findone()  //查询第一条数据
{
        "_id" : objectid("5bd70716a20424a081c010d1"),
        "name" : "zhangsan",
        "age" : 18,
        "sex" : "男"
}
> db.students.find({"name":"xiaohuang"})   //查询name为xiaohuang的数据
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
>
> db.students.find({"age":18}) //查询age为18的数据
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }

> db.students.find({"age":{$gt:18}})//查询age大于18的数据
{ "_id" : objectid("5bd70848a20424a081c010d3"), "name" : "wangwu", "age" : 19, "sex" : "男" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }
{ "_id" : objectid("5bd708b4a20424a081c010d9"), "name" : "小红", "age" : 21, "sex" : "女" }
{ "_id" : objectid("5bd708c0a20424a081c010da"), "name" : "小兰", "age" : 21, "sex" : "女" }

> db.students.find({"age":{$gte:18}})//查询age大于等于18的数据
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd70848a20424a081c010d3"), "name" : "wangwu", "age" : 19, "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }
{ "_id" : objectid("5bd708b4a20424a081c010d9"), "name" : "小红", "age" : 21, "sex" : "女" }
{ "_id" : objectid("5bd708c0a20424a081c010da"), "name" : "小兰", "age" : 21, "sex" : "女" }
> db.students.find({"age":{$lt:18}}) //查询age小于18的数据
{ "_id" : objectid("5bd7086aa20424a081c010d5"), "name" : "xiaolan", "age" : 16, "sex" : "女" }
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }

> db.students.find({"age":{$lte:18}})  //查询age小于等于18的数据
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd7086aa20424a081c010d5"), "name" : "xiaolan", "age" : 16, "sex" : "女" }
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }

> db.students.find({"age":{$gte:18,$lte:20}}) //查询age大于等于18小于等于20的数据 
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd70848a20424a081c010d3"), "name" : "wangwu", "age" : 19, "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }
> db.students.find({$or:[{age:18},{age:20}]})  //查询age为18或则20的数据
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }

> db.students.find({"name":/xiao/}) //查询name中包含xiao的数据
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd7086aa20424a081c010d5"), "name" : "xiaolan", "age" : 16, "sex" : "女" }
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }

> db.students.find({"name":/^小/})  //查询name中以'小'开头的数据
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }
{ "_id" : objectid("5bd708b4a20424a081c010d9"), "name" : "小红", "age" : 21, "sex" : "女" }
{ "_id" : objectid("5bd708c0a20424a081c010da"), "name" : "小兰", "age" : 21, "sex" : "女" }

> db.students.find().sort({"age":1})  //age升序查询数据
{ "_id" : objectid("5bd7086aa20424a081c010d5"), "name" : "xiaolan", "age" : 16, "sex" : "女" }
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd70848a20424a081c010d3"), "name" : "wangwu", "age" : 19, "sex" : "男" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }
{ "_id" : objectid("5bd708b4a20424a081c010d9"), "name" : "小红", "age" : 21, "sex" : "女" }
{ "_id" : objectid("5bd708c0a20424a081c010da"), "name" : "小兰", "age" : 21, "sex" : "女" }

> db.students.find().sort({"age":-1})  //age降序查询数据
{ "_id" : objectid("5bd708b4a20424a081c010d9"), "name" : "小红", "age" : 21, "sex" : "女" }
{ "_id" : objectid("5bd708c0a20424a081c010da"), "name" : "小兰", "age" : 21, "sex" : "女" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }
{ "_id" : objectid("5bd70848a20424a081c010d3"), "name" : "wangwu", "age" : 19, "sex" : "男" }
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd7086aa20424a081c010d5"), "name" : "xiaolan", "age" : 16, "sex" : "女" }
> db.students.find({"age":{$gt:18}}).count()  //查询age大于18的数据的数量
4

> db.students.find({},{"name":1,"sex":1})   //只查询显示name 和sex这两项
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "sex" : "男" }
{ "_id" : objectid("5bd70848a20424a081c010d3"), "name" : "wangwu", "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "sex" : "女" }
{ "_id" : objectid("5bd7086aa20424a081c010d5"), "name" : "xiaolan", "sex" : "女" }
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "sex" : "男" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "sex" : "男" }
{ "_id" : objectid("5bd708b4a20424a081c010d9"), "name" : "小红", "sex" : "女" }
{ "_id" : objectid("5bd708c0a20424a081c010da"), "name" : "小兰", "sex" : "女" }
>
> db.students.find({"name":"xiaohuang"},{"name":1,"sex":1})  //查询name为xiaohuang,并只显示name和sex
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "sex" : "男" }
>
> db.students.find().limit(3)  //查询前三条数据
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd70848a20424a081c010d3"), "name" : "wangwu", "age" : 19, "sex" : "男" }

> db.students.find().skip(3)  //跳过前三条数据查询
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd7086aa20424a081c010d5"), "name" : "xiaolan", "age" : 16, "sex" : "女" }
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }
{ "_id" : objectid("5bd708b4a20424a081c010d9"), "name" : "小红", "age" : 21, "sex" : "女" }
{ "_id" : objectid("5bd708c0a20424a081c010da"), "name" : "小兰", "age" : 21, "sex" : "女" }

修改

> db.students.find()
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "zhangsan", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd70848a20424a081c010d3"), "name" : "wangwu", "age" : 19, "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd7086aa20424a081c010d5"), "name" : "xiaolan", "age" : 16, "sex" : "女" }
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }
{ "_id" : objectid("5bd708b4a20424a081c010d9"), "name" : "小红", "age" : 21, "sex" : "女" }
{ "_id" : objectid("5bd708c0a20424a081c010da"), "name" : "小兰", "age" : 21, "sex" : "女" }
> db.students.update({"name":"zhangsan"},{"name":"new zhangsan"})   
//第一个{},里面是匹配的,第二个是修改  不用$set 会直接覆盖掉
writeresult({ "nmatched" : 1, "nupserted" : 0, "nmodified" : 1 })
> db.students.find()
{ "_id" : objectid("5bd70716a20424a081c010d1"), "name" : "new zhangsan" }
{ "_id" : objectid("5bd707dca20424a081c010d2"), "name" : "lisi", "age" : 18, "sex" : "男" }
{ "_id" : objectid("5bd70848a20424a081c010d3"), "name" : "wangwu", "age" : 19, "sex" : "男" }
{ "_id" : objectid("5bd7085aa20424a081c010d4"), "name" : "xiaohong", "age" : 18, "sex" : "女" }
{ "_id" : objectid("5bd7086aa20424a081c010d5"), "name" : "xiaolan", "age" : 16, "sex" : "女" }
{ "_id" : objectid("5bd70883a20424a081c010d6"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd70885a20424a081c010d7"), "name" : "xiaohuang", "age" : 17, "sex" : "男" }
{ "_id" : objectid("5bd7089ea20424a081c010d8"), "name" : "小黄", "age" : 20, "sex" : "男" }
{ "_id" : objectid("5bd708b4a20424a081c010d9"), "name" : "小红", "age" : 21, "sex" : "女" }
{ "_id" : objectid("5bd708c0a20424a081c010da"), "name" : "小兰", "age" : 21, "sex" : "女" }
> db.students.update({"name":"xiaohuang"},{$set:{"name":"小黄黄"}}) 
> //会匹配上第一个xiaohuang 并设置name为小黄黄 

>db.students.remove({"name":"小黄黄"})  //删除匹配上的所有name值为小黄黄的 
										//如果只想修改匹配上的第一个  必须添加第二个参数 :
>db.students.remove({"name":"小黄黄"},{justone:true})

查询数据库和数据库里面的表(集合)相关操作

> show dbs   //查询有哪些数据库
admin  0.000gb
class  0.000gb
local  0.000gb
> use class //先用哪个数据库
switched to db class
> show collections   //再查询这个数据库里面有那些集合(表)
students 

db.dropdatabase();  //删除当前所在的数据库 
db.students.drop();  //删除集合  


> use class
switched to db class
> show collections   //查询class数据里面有几个表
students
> db.students.drop()   //删除students这个表
true
> show collections  //没有表,什么也不显示
> db.dropdatabase()  //删除当前所在的数据库
{ "dropped" : "class", "ok" : 1 }
> show dbs   
admin  0.000gb
local  0.000gb
>