连接数据库
本地连接
连接本地数据库,默认端口为 27017
mongo
指定端口连接数据库
mongo --port 28015
远程连接
mongo "mongodb://mongodb0.example.com:28015"
mongo --host mongodb0.example.com:28015
mongo --host mongodb0.example.com --port 28015
密码连接
# mongodb://<user>@<host>:<port>/?authSource=<db>
mongo "mongodb://alice@mongodb0.examples.com:28015/?authSource=admin"
# --username <user> --password --authenticationDatabase <db>
mongo --username alice --password --authenticationDatabase admin --host mongodb0.examples.com --port 28015
连接到副本集
URI连接
# <host1>:<port1>,<host2>:<port2>/?replicaSet=<replica set name>
mongo
"mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA"
DNS连接
增加+srv
开启SSL加密传输
mongo "mongodb+srv://server.example.com/"
命令行选项
# --host <replica set name>/<host1>:<port1>,<host2>:<port2>,...
mongo --host replA/mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017
TLS/SSL 加密连接
URI连接
mongo "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA&ssl=true"
命令行选项
mongo --ssl --host replA/mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017
数据库操作
显示当前数据库
db
数据库不存在则创建,存在则切换为当前数据库
use mydb
显示所有数据库,没有数据的数据库不显示
show dbs
删除当前数据库
db.dropDatabase()
集合操作
创建集合
创建一个集合空间大小为6142800,文档最大个数为10000的固定集合
db.createCollection("myCollection", { capped : true, autoIndexId : true, size :
6142800, max : 10000 })
在插入文档时,如果集合不存在,会创建集合
> db.myCollection.insertOne({name:"Pikaman",gender:1})
{
"acknowledged" : true,
"insertedId" : ObjectId("5e83f327ff9e47416495ae04")
}
查看集合
当前库的所有集合
> show collections
myCollection
查找集合的所有文档
> db.myCollection.find()
{ "_id" : ObjectId("5e83f327ff9e47416495ae04"), "name" : "Pikaman", "gender" : 1 }
文档JSON格式化
> db.myCollection.find().pretty()
{
"_id" : ObjectId("5e83f327ff9e47416495ae04"),
"name" : "Pikaman",
"gender" : 1
}
删除集合
> db.myCollection.drop()
true
文档操作
插入文档
方法 | 说明 |
---|---|
db.collection.insertOne() | 插入一条文档 |
db.collection.insertMany() | 插入多条文档 |
db.collection.insert() | 插入多条或者一条文档 |
插入一条文档
> db.myCollection.insertOne({name:"Pikaman",gender:1})
{
"acknowledged" : true,
"insertedId" : ObjectId("5e83f327ff9e47416495ae04")
}
插入多条文档
> db.myCollection.insertMany([{name:"Tinker",gender:2},{name:"Bell",gender:1}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5e8411fcff9e47416495ae06"),
ObjectId("5e8411fcff9e47416495ae07")
]
}
插入一条或者多条文档
> db.myCollection.insert({name:"Tinkerbell",gender:2})
WriteResult({ "nInserted" : 1 })
> db.myCollection.insert([{name:"Man",gender:2},{name:"Pika",gender:1}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2, // 插入两条数据
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
嵌套插入数据
db.myCollection.insert({
"name": {
"first": "Pika",
"last": "man"
},
"gender": {
"$numberInt": "1"
}
})
查询文档
方法 | 说明 |
---|---|
db.collection.findOne() | 查询单条数据 |
根据条件查询数据
db.myCollection.findOne({"name":"Pikaman"})
db.myCollection.findOne({"name.first":"Pika"})
db.bios.find( { birth: { $gt: new Date('1940-01-01'), $lt: new Date('1960-01-01') } } )
更新文档
删除文档
参考:
欢迎关注微信公众号:【皮卡战记】
