MongoDB入门

来源:互联网 发布:php 命名空间 大小写 编辑:程序博客网 时间:2024/06/08 09:13

原文地址https://github.com/XanthusL/blog-gen

MongoDB入门

  • insert
  • find
  • update
  • remove
  • explain
  • index
  • 主从复制
  • 读写分离
  • golang mgo

安装

在docker中使用,因此:

docker pull mongo

可能是网络不好,在EOF错误出现数次后成功
启动mongo

docker run -p 27017:27017 mongo

连接到mongo, 192.168.1.102为本机ip, 需要 -i 参数(交互模式)

docker run -it mongo mongo --host 192.168.1.102

首次直接run,之后就可以docker start containerId

一些概念(个人理解)

  • 数据库,同关系型数据库中的数据库
  • 集合,关系型数据库中的表
  • 文档,关系型数据库中的记录、行

help

> help    db.help()                    help on db methods    db.mycoll.help()             help on collection methods    sh.help()                    sharding helpers    rs.help()                    replica set helpers    help admin                   administrative help    help connect                 connecting to a db help    help keys                    key shortcuts    help misc                    misc things to know    help mr                      mapreduce    show dbs                     show database names    show collections             show collections in current database    show users                   show users in current database    show profile                 show most recent system.profile entries with time >= 1ms    show logs                    show the accessible logger names    show log [name]              prints out the last segment of log in memory, 'global' is default    use <db_name>                set current database    db.foo.find()                list objects in collection foo    db.foo.find( { a : 1 } )     list objects in foo where a == 1    it                           result of the last line evaluated; use to further iterate    DBQuery.shellBatchSize = x   set default number of items to display on shell    exit                         quit the mongo shell> 

准备

创建和选择数据库

CRUD

insert

在关系型数据库中(以MySql为例),通常是CREATE DB_NAME创建数据库;USE DB_NAME选择数据库;show databases查看数据库列表。
mongo中查看数据库是show dbs,创建和选择都是use db_name

> show dbsadmin  0.000GBlocal  0.000GB>use play_groundswitched to db play_ground> db.first_collection.insert({"name":"liyiheng"})WriteResult({ "nInserted" : 1 })> show dbsadmin        0.000GBlocal        0.000GBplay_ground  0.000GB> db.first_collection.insert({"name":"liyiheng1"})WriteResult({ "nInserted" : 1 })> db.first_collection.insert({"name":"liyiheng2","age":123})WriteResult({ "nInserted" : 1 })> db.first_collection.insert({"name":"liyiheng3","age":11})WriteResult({ "nInserted" : 1 })> 

use play_ground是切换到数据库play_ground,如果不存在则创建。
db.first_collection.insert({"name":"liyiheng1"})是在集合first_collection中插入文档{"name":"liyiheng1"},如果集合不存在就创建。

find

db.集合名.find()返回所有结果,类似SELECT * FROM some_table
find()中参数为查询条件

> db.first_collection.find({"name":"liyiheng2"}){ "_id" : ObjectId("58db67addbb9744fab3b1d9b"), "name" : "liyiheng2", "age" : 123 }

find({"name":"liyiheng2"}),即查询nameliyiheng2的文档。

关键字:$gt > , $gte >= , $lt < , $lte <= , $ne != ,$or, $in$nin
栗子:

> db.first_collection.find({"age":{$gt:100}}){ "_id" : ObjectId("58db6716dbb9744fab3b1d99"), "name" : "liyiheng", "age" : 6666 }{ "_id" : ObjectId("58db67addbb9744fab3b1d9b"), "name" : "liyiheng2", "age" : 123 }

高端点的用法:
- 正则 db.first_collection.find({“name”:/^l/)
- where db.first_collection.find({where:function(){return this.age > 100})

update

> db.first_collection.update({"name":"liyiheng"},{"age":666})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.first_collection.find(){ "_id" : ObjectId("58db6716dbb9744fab3b1d99"), "age" : 666 }{ "_id" : ObjectId("58db679fdbb9744fab3b1d9a"), "name" : "liyiheng1" }{ "_id" : ObjectId("58db67addbb9744fab3b1d9b"), "name" : "liyiheng2", "age" : 123 }{ "_id" : ObjectId("58db67b7dbb9744fab3b1d9c"), "name" : "liyiheng3", "age" : 11 }

原文档中的name字段没了,也就是整个原文档被{"age":666}覆盖。
局部更新:

> db.first_collection.update({"name":"liyiheng"},{$set:{"age":888}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.first_collection.find({"name":"liyiheng"}){ "_id" : ObjectId("58db6716dbb9744fab3b1d99"), "name" : "liyiheng", "age" : 888 }> db.first_collection.update({"name":"liyiheng"},{$inc:{"age":111}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.first_collection.find({"name":"liyiheng"}){ "_id" : ObjectId("58db6716dbb9744fab3b1d99"), "name" : "liyiheng", "age" : 999 }

其他用法:
- update(arg0, arg1, true)
表示若没有符合arg0的文档,则插入一条新的
- update(arg0, arg1, arg2, true)
表示若有多条文档符合arg0,则全部更新

remove

remove()
remove(arg0)

explain

ensureIndex

dropIndexes

To be continued

原创粉丝点击