MongoDB快速入门(Mac)
安装与启动
先去下载吧
下载解压之后打开终端进入bin
目录内
先把mongod
丢进终端运行一下,再把mongo
丢进去运行,注意顺序不可以颠倒哈
如果以上步骤完成的话,可以输入1+1
然后回车,如果返回2的话就说明成功启动了
创建数据库
use
方法
use
库名
在 MongoDB
中默认数据库是:test
如果还没有创建过任何数据库,则集合/文档将存储在test
数据库中
如果库名不存在,则该命令将创建一个新的数据库,否则返回使用现有的数据库
db
显示当前选择的数据库库名
show dbs
显示当前数据库列表
删除数据库
dropDatabase
方法
db.dropDatabase()
删除当前所选数据库,如果没有选择任何数据库,那么它将删除默认的test
数据库
配合use
库名 使用,删除数据库
创建集合
createCollection
方法
db.createCollection(name,options)
name
是要创建的集合名,为必填项
参数 | 类型 | 描述 |
---|---|---|
name | String | 是要创建的集合名,为必填项 |
options | Document | 是用于指定集合配置的一个文档,为可选项 |
options
是用于指定集合配置的一个文档,为可选项
db.createCollection("xxx", {
capped : true,
autoIndexId : true,
size : 6142800,
max : 10000
})
show collections
检查创建的集合
在
MongoDB
中,不需要创建集合,当插入一些文档时,MongoDB
会自动创建集合
参数 | 类型 | 描述 |
---|---|---|
capped | String | 默认false !如果为true,则启用封闭的集合,上限集合是固定大小的集合,它在达到其最大大小时自动覆盖其最旧的条目 !如果指定true,则还需要指定size参数 |
autoIndexId | Document | 默认值为false !如果为true,则在_id字段上自动创建索引 |
size | number | 默认值为false 指定上限集合的最大大小(以字节为单位) !如果capped为true,那么还需要指定此字段的值 |
max | number | 指定上限集合中允许的最大文档数 |
删除集合
drop
方法
db.collection.drop()
collection
为要删除的集合名(下同)
如果选定的集合成功删除,drop()方法将返回true,否则返回false
插入文档
insert
方法
db.collection.insert(document)
save
方法
db.collection.save(document)
db.collection.insert(
{
id_:1,
key:'val',
key:[
'val1',
'val2',
'val3'
]
}
)
若不在文档中指定_id
,那么insert
和save
方法一样,将会自动分配id
若在文档中指定_id
那么save
方法将会替换当前包含该_id
文档的全部数据,而insert
将会报错
若在文档中指定_id
存在,insert
方法将会报错,save
则会覆盖
insertOne
方法
db.collection.insertOne()
单个文档插入到集合中
db.xxx.insertOne(
{
key:val
}
)
insertMany
方法
db.collection.insertMany()
多个文档插入到集合中
db.xxx.insertMany(
[
{key1:val1},
{key2.val2}
]
)
查询文档
find
方法
db.collection.find(document)
将以非结构化的方式显示所有文档
pretty
方法
db.collection.find().pretty()
以结构化的方式显示所有文档
findOne
方法
db.collection.findOne()
只返回一个文档
and
方法
过滤查询
返回
key1=val1
且key2=val2
的所有文档
db.collection.find(
{
$and:[
{key1:val1},
{key2:val2}
]
}
).pretty()
or
方法
返回
key1=val1
或key2=val2
的所有文档
db.collection.find(
{
$or:[
{key1:val1},
{key2:val2}
]
}
).pretty()
and
混合or
方法
返回
key
大于50
且key1
等于val1
或key2
等于val2
的所有文档
db.collection.find(
{
key:{$gt:50},
$or:[
{key1:val1},
{key2:val2}
]
}
).pretty()
运算符查询
操作 | 语法 | 示例 |
---|---|---|
相等 | : | db.collection.find({“key”:”val”}).pretty() |
小于 | lt | db.collection.find({“likes”:{$lt:50}}).pretty() |
大于 | gt | db.collection.find({“likes”:{$gt:50}}).pretty() |
小于等于 | lte | db.collection.find({“likes”:{$lte:50}}).pretty() |
大于等于 | gte | db.collection.find({“likes”:{$gte:50}}).pretty() |
不等于 | ne | db.collection.find({“likes”:{$ne:50}}).pretty() |
嵌入文档查询
db.collection.insertMany( [
{ name: "warren", age: 25, size: { h: 14, w: 21, uom: "cm" }, score: "A" },
{ name: "anthony", age: 50, size: { h: 8.5, w: 11, uom: "in" }, score: "A" },
{ name: "ben", age: 100, size: { h: 8.5, w: 11, uom: "in" }, score: "D" },
{ name: "glen", age: 75, size: { h: 22.85, w: 30, uom: "cm" }, score: "D" },
{ name: "carl", age: 45, size: { h: 10, w: 15.25, uom: "cm" }, score: "A" }
]);
整个嵌入式文档中相等匹配需要精确匹配指定的
val
文档,包括字段顺序
正确:db.collection.find( { size : { h : 14 , w : 21 , uom : " cm " } } )
✅
错误:db.collection.find( { size : { w : 21 , h : 14 , uom : " cm " } } )
❌
db.collection.find( { " size.uom " : " in " } )
返回collection
中字段为size
的嵌套字段uom
等于in
的所有文档
嵌入文档混合运算符查询
db.collection.find( { "size.h" : { $lt : 15 } } )
返回collection
中字段为size
的嵌套字段h
中值小于15
的所有文档
嵌入文档混合运算符以及and方法查询
db.collection.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
返回collection
中字段为size
的嵌套字段h
中值小于15
,uom
嵌套字段等于in
,score
嵌套字段等于d
的所有文档
更新文档
update
方法
db.collection.update( { " key " : " old_val " } , { $set : { " key " : " new_val " } } )
默认情况下,MongoDB
只会更新一个文档。要更新多个文档,需要将参数multi
设置为true
。
db.collection.update(
{ "key" : "old_val" } ,
{ $set :
{ "key" : "new_val" }
} ,
{ multi:true } )
save
方法
db.collection.save( { _id : ObjectId() , new_data } )
save
将文档数据替换现有文档
db.collection.save(
{
"_id" : 100, "key":"val", "key":"val"
}
)
删除文档
remove
方法
db.collection.remove( { 'key' : 'val' } )
只删除第一条记录
db.collection.remove( { 'key' : 'val' } , 1 )
投影(选择字段)
在MongoDB
中,当执行find
方法时,它默认将显示文档的所有字段
为了限制显示的字段,需要将字段列表对应的值设置为1
或0
1
表示显示字段,0
表示隐藏字段。
db.collection.find( {}, { key : 1 } )
限制记录数
limit
方法
db.collection.find().limit( number )
如果没有在limit
方法中指定number
参数的值,那么它将显示集合中的所有文档
skip
方法
db.collection.find().skip( number )
用于跳过文档数量
skip
方法默认值为0
sort
方法
db.collection.find().sort( { key : 1 } )
使用指定排序顺序1
和-1
1
用于升序,而-1
用于降序。
参考文档.