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阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容