时间:2021-07-01 10:21:17 帮助过:8人阅读
当你看到这个说明你的mogodb已经安装完成。
略
windows很容易
去官网下个包,无脑下一步即可。
docker部署很容易:参考 https://hub.docker.com/r/tutum/mongodb/ 这个镜像
如果还不知道docker是什么,参考之前的帖子。或者百度自行安装体验。
它是一种 NoSQL (not only sql)非关系型数据库
开源且跨平台
服务器可以集群以平衡服务器的压力
支持高并发的读写和海量存储
数据存储没有模式
集合(相当于关系型数据里的表)
文档(关系型数据库中的记录)
文档就是一些键值对(类似python里的字典)
同一集合中的文档结构模式与数据类型可以不同
文档中的字符串是区分大小写的
mongod 是服务器端 默认启动了已经。 详细的可以
mongod -h
~$ mongod
2017-07-13T19:24:32.101+0000 I CONTROL [initandlisten] MongoDB starting : pid=26124 port=27017 dbpath=/data/db 64-bit host=jonny-PC
2017-07-13T19:24:32.101+0000 I CONTROL [initandlisten] db version v3.2.11
2017-07-13T19:24:32.101+0000 I CONTROL [initandlisten] git version: 009580ad490190ba33d1c6253ebd8d91808923e4
2017-07-13T19:24:32.101+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2k 26 Jan 2017
2017-07-13T19:24:32.101+0000 I CONTROL [initandlisten] allocator: tcmalloc
2017-07-13T19:24:32.101+0000 I CONTROL [initandlisten] modules: none
2017-07-13T19:24:32.101+0000 I CONTROL [initandlisten] build environment:
2017-07-13T19:24:32.101+0000 I CONTROL [initandlisten] distarch: x86_64
2017-07-13T19:24:32.101+0000 I CONTROL [initandlisten] target_arch: x86_64
2017-07-13T19:24:32.101+0000 I CONTROL [initandlisten] options: {}
2017-07-13T19:24:32.131+0000 E NETWORK [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
2017-07-13T19:24:32.131+0000 E NETWORK [initandlisten] addr already in use
2017-07-13T19:24:32.131+0000 E STORAGE [initandlisten] Failed to set up sockets during startup.
2017-07-13T19:24:32.131+0000 I CONTROL [initandlisten] dbexit: rc: 48
mongo 是客户端
~$ mongo
MongoDB shell version: 3.2.11
connecting to: test
>
直接连上了本地的服务器。
使用数据库和集合前不用建立,执行插入数据时会自动建立
这一点和关系型数据库不同。用之前必须要先创建 数据库和表。否则不行,但是mongodb不一样,用之前不需要创建。
比如我们要使用test数据库
MongoDB shell version: 3.2.11
connecting to: test
> use test
switched to db test
>
提示已经切换到test数据库
再比如我们要用一个stu数据库,其实之前没有,但是直接use stu
> use stu
switched to db stu
只有当我们向这个数据库里插入数据,这个数据库才会真正建立
比如我们现在先 use test 切换到 test数据库
然后插入一条数据:
集合是blogg
> use test
switched to db test
> db.blogg.insert({‘id‘:1,‘title‘:‘myblog‘})
WriteResult({ "nInserted" : 1 })
>
WriteResult,这两个键值对 就插入成功了。
也可以这样,预先定义你要插入的内容
> d = {‘id‘:2,‘title‘:‘yourblog‘}
{ "id" : 2, "title" : "yourblog" }
> db.blogg.insert(d)
WriteResult({ "nInserted" : 1 })
我们查询一下
多插2条,查询:
> db.blogg.insert({‘id‘:2,‘title‘:‘myblog2‘})
WriteResult({ "nInserted" : 1 })
> db.blogg.insert({‘id‘:3,‘title‘:‘myblog3‘})
WriteResult({ "nInserted" : 1 })
> db.blogg.find()
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : 1, "title" : "myblog" }
{ "_id" : ObjectId("5967cc9bf9e6f46246252b7a"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cca0f9e6f46246252b7b"), "id" : 3, "title" : "myblog3" }
注意这里O是大写
> db.blogg.findOne()
{
"_id" : ObjectId("5967cc37f9e6f46246252b79"),
"id" : 1,
"title" : "myblog"
}
比如我只要id为2的
> db.blogg.findOne({‘id‘:2})
{
"_id" : ObjectId("5967cc9bf9e6f46246252b7a"),
"id" : 2,
"title" : "myblog2"
}
> db.blogg.insert({‘id‘:2,‘title‘:‘myblog2‘})
WriteResult({ "nInserted" : 1 })
> db.blogg.find()
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : 1, "title" : "myblog" }
{ "_id" : ObjectId("5967cc9bf9e6f46246252b7a"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cca0f9e6f46246252b7b"), "id" : 3, "title" : "myblog3" }
{ "_id" : ObjectId("5967cd30f9e6f46246252b7c"), "id" : 2, "title" : "myblog2" }
> db.blogg.find({‘id‘:2})
{ "_id" : ObjectId("5967cc9bf9e6f46246252b7a"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cd30f9e6f46246252b7c"), "id" : 2, "title" : "myblog2" }
这里的 "_id "是mongodb 自己创建的。每个文档的id不一样
比如我们 增加或修改指定键值对
修改 id 为1 title 从myblog改成myblog1
> db.blogg.find()
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : 1, "title" : "myblog" }
{ "_id" : ObjectId("5967cc9bf9e6f46246252b7a"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cca0f9e6f46246252b7b"), "id" : 3, "title" : "myblog3" }
{ "_id" : ObjectId("5967cd30f9e6f46246252b7c"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cdc4f9e6f46246252b7d"), "id" : 2, "title" : "yourblog" }
> db.blogg.update({‘id‘:1},{‘$set‘:{‘title‘:‘myblog1‘}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blogg.find()
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : 1, "title" : "myblog1" }
{ "_id" : ObjectId("5967cc9bf9e6f46246252b7a"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cca0f9e6f46246252b7b"), "id" : 3, "title" : "myblog3" }
{ "_id" : ObjectId("5967cd30f9e6f46246252b7c"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cdc4f9e6f46246252b7d"), "id" : 2, "title" : "yourblog" }
若没找到,则新建 键值对:
比如 id为1的 再插入一条 titile-2 ,myblog1-2
> db.blogg.update({‘id‘:1},{‘$set‘:{‘title-2‘:‘myblog1-2‘}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blogg.find({‘id‘:1})
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : 1, "title" : "myblog1", "title-2" : "myblog1-2" }
>
> db.blogg.update({‘id‘:1},{‘$unset‘:{‘title-2‘:‘myblog1-2‘}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blogg.find({‘id‘:1})
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : 1, "title" : "myblog1" }
>
比如修改id 的值,增加可以为负数 比如 -2
> db.blogg.update({‘id‘:1},{‘$inc‘:{‘id‘:-2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blogg.find()
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : -1, "title" : "myblog1" }
{ "_id" : ObjectId("5967cc9bf9e6f46246252b7a"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cca0f9e6f46246252b7b"), "id" : 3, "title" : "myblog3" }
{ "_id" : ObjectId("5967cd30f9e6f46246252b7c"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cdc4f9e6f46246252b7d"), "id" : 2, "title" : "yourblog" }
>
见到了之后,操作后变成了id -1
比如修改id 为 -1 的 记录,发现没有 size 则新建了。
> db.blogg.update({‘id‘:-1},{‘$set‘:{‘size‘:10}},true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blogg.find({‘id‘:-1})
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : -1, "title" : "myblog1", "size" : 10 }
>
比如三个id为2的文档,匹配的3个都被新增了 size 为7 的键值对。
> db.blogg.find()
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : -1, "title" : "myblog1", "size" : 10 }
{ "_id" : ObjectId("5967cc9bf9e6f46246252b7a"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cca0f9e6f46246252b7b"), "id" : 3, "title" : "myblog3" }
{ "_id" : ObjectId("5967cd30f9e6f46246252b7c"), "id" : 2, "title" : "myblog2" }
{ "_id" : ObjectId("5967cdc4f9e6f46246252b7d"), "id" : 2, "title" : "yourblog" }
> db.blogg.update({‘id‘:2},{‘$set‘:{‘size‘:7}},false,true)
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.blogg.find()
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : -1, "title" : "myblog1", "size" : 10 }
{ "_id" : ObjectId("5967cc9bf9e6f46246252b7a"), "id" : 2, "title" : "myblog2", "size" : 7 }
{ "_id" : ObjectId("5967cca0f9e6f46246252b7b"), "id" : 3, "title" : "myblog3" }
{ "_id" : ObjectId("5967cd30f9e6f46246252b7c"), "id" : 2, "title" : "myblog2", "size" : 7 }
{ "_id" : ObjectId("5967cdc4f9e6f46246252b7d"), "id" : 2, "title" : "yourblog", "size" : 7 }
>
> db.blogg.find()
{ "_id" : ObjectId("5967cc37f9e6f46246252b79"), "id" : -1, "title" : "myblog1", "size" :