MongoDB 101

MongoDB 101

參考

Documents

MongoDB中存儲的數據稱為document. documentjson對象類似.
示例:

{
   "_id" : ObjectId("54c955492b7c8eb21818bd09"),
   "address" : {
      "street" : "2 Avenue",
      "zipcode" : "10075",
      "building" : "1480",
      "coord" : [ -73.9557413, 40.7720266 ]
   },
   "borough" : "Manhattan",
   "cuisine" : "Italian",
   "grades" : [
      {
         "date" : ISODate("2014-10-01T00:00:00Z"),
         "grade" : "A",
         "score" : 11
      },
      {
         "date" : ISODate("2014-01-16T00:00:00Z"),
         "grade" : "B",
         "score" : 17
      }
   ],
   "name" : "Vella",
   "restaurant_id" : "41704620"
}

Collections

MongoDBdocuments存儲在collections中. Collections類似于關系數據庫中的table. 但是Collection不要求其中的document有相同的結構.

存儲在Collection中的document必須有一個_id字段, 作為其的primary key.

Import Example Dataset

primer-dataset.json中下載這個數據集合, 另存為primer-dataset.json.

使用mongoimport命令導入該數據集:

mongoimport --db test --collection restaurants --drop --file ~/downloads/primer-dataset.json

運行結果:

2017-11-16T20:17:47.487+0800 I NETWORK  [thread1] connection accepted from 127.0.0.1:34806 #2 (1 connection now open)
2017-11-16T20:17:47.489+0800    connected to: localhost
2017-11-16T20:17:47.489+0800    dropping: test.restaurants
2017-11-16T20:17:47.489+0800 I COMMAND  [conn2] CMD: drop test.restaurants
2017-11-16T20:17:47.531+0800 I NETWORK  [thread1] connection accepted from 127.0.0.1:34808 #3 (2 connections now open)
2017-11-16T20:17:48.569+0800    imported 25359 documents
2017-11-16T20:17:48.569+0800 I -        [conn2] end connection 127.0.0.1:34806 (2 connections now open)
2017-11-16T20:17:48.569+0800 I -        [conn3] end connection 127.0.0.1:34808 (2 connections now open)

Insert a Document

進入mongoDB shell之后, 切換到test數據庫.

use test

插入數據:

db.restaurants.insert(
   {
      "address" : {
         "street" : "2 Avenue",
         "zipcode" : "10075",
         "building" : "1480",
         "coord" : [ -73.9557413, 40.7720266 ]
      },
      "borough" : "Manhattan",
      "cuisine" : "Italian",
      "grades" : [
         {
            "date" : ISODate("2014-10-01T00:00:00Z"),
            "grade" : "A",
            "score" : 11
         },
         {
            "date" : ISODate("2014-01-16T00:00:00Z"),
            "grade" : "B",
            "score" : 17
         }
      ],
      "name" : "Vella",
      "restaurant_id" : "41704620"
   }
)

返回值:

WriteResult({ "nInserted" : 1 })

如果傳遞給insert()document中不包含_id字段, 那么mongo shell會自動設置這個字段.

Query for Documents

Query for all

不帶任何參數的find()將會返回全部documents.

db.restaurants.find()

Specify Equality Conditions

{ <field1>: <value1>, <field2>: <value2>, ... }

如果<field>top-level field, 而且不是一個嵌套的document的字段, 或者數組的字段, 那么字段名可以用引號括起來, 也可以省略引號.

如果<field>在一個嵌套的document里面或者在一個數組中, 可以使用dot notation來訪問該字段, 此時引號是必須的.

Query by a Top Level Field

以下查詢語句返回borough字段為Manhattandocuments.

db.restaurants.find( { "borough": "Manhattan" } )

Query by a Field in an Embedded Document

這個時候引號是必須的:

db.restaurants.find( { "address.zipcode": "10075" } )

Query by a Field in an Array

grades數組包含了嵌套的documents作為它的元素. 如果要指定一個查詢條件在這些documents的字段上, 那么需要使用dot notation.

下面這條查詢語句用于查找grades數組中包含了grade字段為B的所有documents.

db.restaurants.find( { "grades.grade": "B" } )

Specify Conditions with Operators

{ <field1>: { <operator1>: <value1> } }

$gt 大于

db.restaurants.find( { "grades.score": { $gt: 30 } } )

$lt 小于

db.restaurants.find( { "grades.score": { $lt: 10 } } )

Logical AND

如果需要匹配多個conditions, 那么直接用逗號分隔開每個condition document即可.

db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } )

Logical OR

使用$or連接多個conditions:

db.restaurants.find(
   { $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] }
)

Sort Query Results

直接將sort()添加到query后面. 然后在sort()中傳入用于指定排序的document. 其中包含了用于排序的fields, 和對應的排序類型(升序 1, 降序 -1).

以下命令對restaurants的查詢結果首先按照borough字段升序排序, 然后在每一個borough中按照address.zipcode字段升序排列.

db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )

Update Data

update()參數:

  • 一個filter document用于匹配需要被修改的documents
  • 一個update document用于指定需要進行的修改
  • 一個options parameter, 這是可選的參數

filter document的結構和語法同query conditions.
默認情況下, update()僅修改一個document. 使用multi option來更新所有匹配的documents.

_id字段是無法進行修改的.

Update Specific Fields

有一些如$set的操作符會在某個field不存在的時候創建該field.

Update Top-Level Fields

db.restaurants.update(
    { "name" : "Juni" },
    {
      $set: { "cuisine": "American (New)" },
      $currentDate: { "lastModified": true }
    }
)

currentDate

{ $currentDate: { <field1>: <typeSpecification1>, ... } }

<typeSpecification> can be either:

  • a boolean true to set the field value to the current date as a Date, or
  • a document { $type: "timestamp" } or { $type: "date" } which explicitly specifies the type. The operator is case-sensitive and accepts only the lowercase "timestamp" or the lowercase "date".

修改后的對象:

{
    "_id" : ObjectId("5a0d81eb80cddb51a870feb7"),
    "address" : {
        "building" : "12",
        "coord" : [
            -73.9852329,
            40.745971
        ],
        "street" : "East 31 Street",
        "zipcode" : "10016"
    },
    "borough" : "Manhattan",
    "cuisine" : "American (New)",
    "grades" : [
        {
            "date" : ISODate("2014-09-19T00:00:00Z"),
            "grade" : "A",
            "score" : 12
        },
        {
            "date" : ISODate("2013-08-05T00:00:00Z"),
            "grade" : "A",
            "score" : 5
        },
        {
            "date" : ISODate("2012-06-07T00:00:00Z"),
            "grade" : "A",
            "score" : 0
        }
    ],
    "name" : "Juni",
    "restaurant_id" : "41156888",
    "lastModified" : ISODate("2017-11-16T13:04:49.640Z")
}

Update an Embedded Filed

db.restaurants.update(
  { "restaurant_id" : "41156888" },
  { $set: { "address.street": "East 31st Street" } }
)

Update Multiple Documents

將匹配address.zipcode == 10016, cuisine == Otherdocumentcuisine設置為Category To Be Determined, lastModified設置為當前時間.

db.restaurants.update(
  { "address.zipcode": "10016", cuisine: "Other" },
  {
    $set: { cuisine: "Category To Be Determined" },
    $currentDate: { "lastModified": true }
  },
  { multi: true}
)

Replace a Document

_id不能被替換. 傳遞一個全新的document作為第二個參數. 因為Collection中的document是沒有固定的Schema的. 所以新的document可以有不用于之前documentfields.

如果新的document中有_id字段, 那么必須和原來的一樣; 或者直接不要帶上_id.

update之后, 這個document僅包含第二個參數document的字段了.

db.restaurants.update(
   { "restaurant_id" : "41704620" },
   {
     "name" : "Vella 2",
     "address" : {
              "coord" : [ -73.9557413, 40.7720266 ],
              "building" : "1480",
              "street" : "2 Avenue",
              "zipcode" : "10075"
     }
   }
)

如果update操作沒有匹配任何一條數據, 那么默認update什么也不會做. 可以指定upsert option為true, 使得在這種情況下, update直接創建一個新的document.

MongoDB中, 寫操作是原子性的, 但是僅僅是對于單個document. 如果一個update操作會修改多個documents, 那么這些操作將會和其他對這個collection的寫操作交錯進行.

Remove Data

Remove All Documents That Match a Condition

db.restaurants.remove( { "borough": "Manhattan" } )

justOne Option

使用justOne: true選項使得僅移除一個匹配的documents.

db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )

Remove All Documents

db.restaurants.remove( { } )

Drop a Collection

remove僅僅移除collection中的document. 如果需要移除collection本身, 以及它的indexes等, 直接用下面語句即可:

db.restaurants.drop()

Data Aggregation

The aggregate() method accepts as its argument an array of stages, where each stage, processed sequentially, describes a data processing step.

db.collection.aggregate( [ <stage1>, <stage2>, ... ] )

Group Documents by a Field and Calculate Count

使用$group stage來通過key聚合數據. 通過_id來指定$group stage用到的field. $group通過field path來訪問fields, 格式就是在field名字前加上美元$符號.

The following example groups the documents in the restaurants collection by the borough field and uses the $sum accumulator to count the documents for each group.

db.restaurants.aggregate(
   [
     { $group: { "_id": "$borough", "count": { $sum: 1 } } }
   ]
);

結果:

{ "_id" : "Missing", "count" : 51 }
{ "_id" : "Staten Island", "count" : 969 }
{ "_id" : "Brooklyn", "count" : 6086 }
{ "_id" : "Bronx", "count" : 2338 }
{ "_id" : "Queens", "count" : 5656 }
{ "_id" : "Manhattan", "count" : 10260 }

The _id field contains the distinct borough value, i.e., the group by key value.

Filter and Group Documents

db.restaurants.aggregate(
   [
     { $match: { "borough": "Queens", "cuisine": "Brazilian" } },
     { $group: { "_id": "$address.zipcode" , "count": { $sum: 1 } } }
   ]
);

先用$match挑出后續$group stage的操作對象. $match的語法同query的語法.

Indexes

如果沒有Indexes, MongoDB必須掃描整個collection來匹配query statement.

createIndex()用于在collection上建立索引. MongoDB自動對_id字段創建索引 (在這個collection建立時).

創建Indexes的語法: 傳遞一個index key specification documentcreateIndex()即可.

{ <field1>: <type1>, ...}

<type>:

  • 1 升序index
  • -1 降序index

createIndex()僅在index不存在的時候創建index.

Create a Single-Field Index

db.restaurants.createIndex( { "cuisine": 1 } )

結果:

2017-11-16T21:57:28.148+0800 I INDEX    [conn4] build index done.  scanned 25360 total records. 0 secs
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}

Create a compound index

db.restaurants.createIndex( { "cuisine": 1, "address.zipcode": -1 } )

fields的順序決定了index如何存儲它的keys. 上面的命令建立indexcuisine field 和 address.zipcode field.

The index orders its entries first by ascending "cuisine" values, and then, within each "cuisine", by descending "address.zipcode" values.

輸出:

2017-11-16T22:01:53.380+0800 I INDEX    [conn4] build index done.  scanned 25360 total records. 0 secs
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 3,
    "ok" : 1
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,546評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,570評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,505評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,017評論 1 313
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,786評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,219評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,287評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,438評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,971評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,796評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,995評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,540評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,230評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,918評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,697評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內容