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
}