MongoDB入門

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

MongoDB入門

安裝

在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 dbs
admin  0.000GB
local  0.000GB
>use play_ground
switched to db play_ground
> db.first_collection.insert({"name":"liyiheng"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin        0.000GB
local        0.000GB
play_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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章