MongoDB 101
參考
Documents
在MongoDB
中存儲的數據稱為document
. document
和json
對象類似.
示例:
{
"_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
MongoDB
將documents
存儲在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
字段為Manhattan
的documents
.
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
- query-comparison
-
query operators
除了$or
,$and
等一些操作符外, 使用操作符的基本形式如下:
{ <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: { <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 == Other
的document
的cuisine
設置為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
可以有不用于之前document
的fields
.
如果新的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 document
到createIndex()
即可.
{ <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
. 上面的命令建立index
于cuisine
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
}