安装完成后在/bin文件夹下打开命令窗口
输入.mongo启动数据库,若正常启动说明安装成功:
为了启动mongodb方便,将mongod.exe路径加入环境变量,电脑->属性->高级系统设置->环境变量,在path里加入路径,即可在命令行任何地方都能用到mongod命令。
开始
在自定义文件夹下新建data和log文件夹,若指定直接启动则会在安装目录的data和log文件夹中生成相关文件。
在log文件夹下新建mongodb.log文件,指定log文件
开启服务并指定端口和日志文件目录
mongod --dbpath F: est
odemongodata --port 27011 --logpath F: est
odemongologmongodb.log
shell操作
开启指定客户端 :mongo 127.0.0.1:27011
显示当前所有数据库及大小:show dbs
使用某个数据库,若没有则直接创建:use tezt01
显示当前数据库的名字:db
当插入一条数据后一个集合才算真正的创建
向集合中插入数据:
> use school
switched to db school
> db.student.insert({"name":"adoctors"});
WriteResult({ "nInserted" : 1 })
列出当前使用的集合
> show collections
student
查找数据
//查找一个集合中的所有数据
> db.student.find()
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "adoctors" }
{ "_id" : ObjectId("5b66cc598276da765b977347"), "name" : "adoctor2s", "age" : 18 }
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28 }
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68 }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26 }
//按条件查找
> db.student.find({"name":"adoctor3s"})
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28 }
//查找年龄大于20的数据
> db.student.find({"age":{$gt:20}})
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28 }
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68 }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26 }
//或
> db.student.find({$or:[{"name":"adoctors"},{"age":26}]})
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "adoctors" }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26 }
//排序,-1降序;1升序
> db.student.find().sort({"age":-1})
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68 }
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28 }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26 }
{ "_id" : ObjectId("5b66cc598276da765b977347"), "name" : "adoctor2s", "age" : 18 }
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "adoctors" }
修改
//update
> db.student.update({"name":"adoctors"},{$set:{"age":16}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "adoctors", "age" : 16 }
{ "_id" : ObjectId("5b66cc598276da765b977347"), "name" : "adoctor2s", "age" : 18 }
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28 }
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68 }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26 }
批量修改,加入{multi:true}
> db.student.update({}, {$set:{"like":"basketball"}},{multi:true})
WriteResult({ "nMatched" : 6, "nUpserted" : 0, "nModified" : 6 })
> db.student.find()
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "adoctors", "age" : 16, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc598276da765b977347"), "name" : "adoctor2s", "age" : 18, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26, "like" : "basketball" }
{ "_id" : ObjectId("5b66d0188276da765b97734b"), "name" : "adoctors", "like" : "basketball" }
替换
//只替换第一个符合条件的数据,且是整条数据都会被替换
> db.student.update({"name":"adoctors"},{"name":"abc"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "abc" }
{ "_id" : ObjectId("5b66cc598276da765b977347"), "name" : "adoctor2s", "age" : 18, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26, "like" : "basketball" }
{ "_id" : ObjectId("5b66d0188276da765b97734b"), "name" : "adoctors", "like" : "basketball" }
删除
//默认删除所有符合条件的整条数据
db.student.remove({"name":"adoctors"})
//删除一个符合条件的整条数据
db.student.remove({"name":"adoctors"},{justOne:true})
limit&&skip
//只查找两条数据
db.student.find().limit(2)
//跳过2条后查找2条数据
db.student.find().limit(2).skip(2)
整个文档集合的信息:db.student.stats()
总条数:db.student.count()
从数据库中删除集合:db.student.drop()