MongoDB安装和基本操作
实验过程
安装
安装完成后通过cd命令进入到相应的文件夹目录下,通过dir命令查看文件夹下的命令
直接使用mongo命令测试,发现连接失败,原因:需要先开启mongod服务
把mongodb安装为windows服务(需要切换到管理员模式下)
并且尝试连接到mongo服务器
基本操作
创建数据库
给数据库添加一个表格,并在集合当中添加记录
> db.createCollection("Student")
{ "ok" : 1 }
修改数据表名
> db.Student.renameCollection("Studentt1")
{ "ok" : 1 }
查看所有的数据库和数据库中所有的文档
> show dbs
admin 0.078GB
lab 0.078GB
local 0.078GB
> show collections
Student
system.indexes
插入一条数据,以及查询一条数据
> db.Student.insert({name:"Lin",age:20,sex:"male"})
WriteResult({ "nInserted" : 1 })
> db.Student.find()
{ "_id" : ObjectId("5a20fc5b214987ceea946458"), "name" : "Lin", "age" : 20, "sex" : "male" }
可以看到mongo自动分配了_id和ObjectId
查询一条数据
> db.Student.findOne()
{
"_id" : ObjectId("5a20fc5b214987ceea946458"),
"name" : "Lin",
"age" : 20,
"sex" : "male"
}
修改数据,发现数据库属性对大小写敏感,age和Age视为2个不同属性,通过update进行更新时,没有该属性会自动添加
> db.Student.update({name:"Lin"},{"$set":{"Age":21}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.Student.findOne()
{
"_id" : ObjectId("5a20fc5b214987ceea946458"),
"name" : "Lin",
"age" : 20,
"sex" : "male",
"Age" : 21
}
删除记录,删除之后再查询发现数据库中数据为null
> db.Student.remove({name:"Lin"})
WriteResult({ "nRemoved" : 1 })
> db.Student.findOne()
null

被折叠的 条评论
为什么被折叠?



