安装
# 安装
$ sudo apt-get install mongodb
# 下面几步一般不用做,列出是为了以防万一
$ sudo systemctl status mongodb.service
$ sudo systemctl enable mongodb.service
$ sudo systemctl stop mongodb.service
$ sudo vim /etc/mongodb.conf # 默认的配置文件位置
$ sudo systemctl start mongodb.service
# 启动客户端
$ mongo
登陆
第一种 (类似 MySql)
客户端连接时,指定用户名,密码,db名称
mongo --port 27017 -u "adminUser" -p "adminPass" --authenticationDatabase "admin"
第二种
客户端连接后,再进行验证
mongo --port 27017
use admin
db.auth("adminUser", "adminPass")
// 输出 1 表示验证成功
基础操作1
// db
show dbs
db // 查看当前数据库
use playground // 切换和创建都可以用这个命令
db.dropDatabase() // 删除当前数据库
// collection
show collections // 同 show tables
db.people.insert({"name":"harryhare","addr":"first street"}) //
db.number.insert([{"n":1},{"n":2}]) // 多个数据用js数组表示
db.people.find({})
db.people.remove({}) // 清空
db.people.drop() // 删表
insert
— | multi | |
---|---|---|
insert | true | |
save | true |
update 的几种方式 2
— | multi | 原子性 |
---|---|---|
update | true | true |
findAndModify | x | true |
save | x | true |
// update
> db.number.update({n:4},{$set:{n:5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.number.find()
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 5 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71baf"), "n" : 1 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71bb0"), "n" : 3 }
> db.number.update({n:5},{"_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 4})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.number.find()
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 4 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71baf"), "n" : 1 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71bb0"), "n" : 3 }
// findAndModify
> db.number.findAndModify({query:{n:4},update:{$set:{n:5}}})
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 4 }
> db.number.find()
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 5 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71baf"), "n" : 1 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71bb0"), "n" : 3 }
> db.number.findAndModify({query:{n:5},update:{"_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 4}})
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 5 }
> db.number.find()
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 4 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71baf"), "n" : 1 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71bb0"), "n" : 3 }
// 返回修改后的值
> db.number.findAndModify({query:{n:4},update:{$set:{n:5}},new:1})
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 5 }
> db.number.find()
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 5 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71baf"), "n" : 1 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71bb0"), "n" : 3 }
// save
> db.number.save({"_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 4 })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.number.find()
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 4 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71baf"), "n" : 1 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71bb0"), "n" : 3 }
分页 3
sort skip limit
> db.number.find().sort({n:1})
{ "_id" : ObjectId("5bba5b9531cccc5270d71baf"), "n" : 1 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71bb0"), "n" : 3 }
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 4 }
> db.number.find().sort({n:-1})
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 4 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71bb0"), "n" : 3 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71baf"), "n" : 1 }
> db.number.find().sort({n:-1}).limit(2)
{ "_id" : ObjectId("5bba5b7431cccc5270d71bae"), "n" : 4 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71bb0"), "n" : 3 }
> db.number.find().sort({n:-1}).skip(1).limit(3)
{ "_id" : ObjectId("5bba5b9531cccc5270d71bb0"), "n" : 3 }
{ "_id" : ObjectId("5bba5b9531cccc5270d71baf"), "n" : 1 }
索引操作4
// 创建
db.people.createIndex({name:1}) // 也可以用ensureIndex, 后者是前者的别名
db.people.createIndex({name:1},{name:"index_name"}) // 可以在第二个参数中指定名字
// 查看
db.people.getIndexes()
// 删除
db.people.dropIndex({name:1})
db.people.dropIndex("index_name") // 用index 的 name 删也可以
创建索引时,这里用的是{name
:1},显然 key 是column 的名字,这里value 可以是 1,-1,“text”,分别是升序索引,降序索引,和文字检索索引
导入导出
# 导出到目录
mongodump -d playground -o db
mongorestore db
function 5
相当于存储过程?
> db.system.js.save({
... _id: "hello",
... value: function(){
... return "hello";
... }
... })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.eval("hello()")
hello
> db.system.js.insert({
... _id: "add_to_number",
... value: function(n){
... db.number.insert({"n":n})
... }
... })
WriteResult({ "nInserted" : 1 })
> db.eval("add_to_number(9)")
null
> db.number.find()
{ "_id" : ObjectId("5bbb6449d7a20e499dc4cd6a"), "n" : 7 }
{ "_id" : ObjectId("5bbb6449d7a20e499dc4cd6b"), "n" : 8 }
{ "_id" : ObjectId("5bbb68616a49c17667a016a1"), "n" : 9 }
其他
覆盖索引查询6
explain 会有个indexOnly
字段,如果 查询结果的列 是 索引的列 的 子集
则查询将只使用索引。
传说中索引在内存中,所有这种查询会快一些。
> db.people.find({name:"harryhare"},{name:1}).explain()
{
...
"indexOnly" : false,
...
}
> db.people.find({name:"harryhare"},{name:1,_id:0}).explain()
{
...
"indexOnly" : true,
...
}
ObjectId 时间戳 字符串 7
> p = db.people.findOne({},{_id:1})
{ "_id" : ObjectId("5bba5b3231cccc5270d71bad") }
> p._id.getTimestamp()
ISODate("2018-10-07T19:14:58Z")
> p._id.str
5bba5b3231cccc5270d71bad
mongo 自动填充时间字段 8,9
db.users.update(
{ _id: 1 },
{
$currentDate: {
lastModified: true,
"cancellation.date": { $type: "timestamp" }
},
$set: {
status: "D",
"cancellation.reason": "user request"
}
}
)
{
"_id" : 1,
"status" : "D",
"lastModified" : ISODate("2014-09-17T23:25:56.314Z"),
"cancellation" : {
"date" : Timestamp(1410996356, 1),
"reason" : "user request"
}
}
www.runoob.com/mongodb/mongodb-databases-documents-collections.html ↩︎
https://stackoverflow.com/questions/10778493/whats-the-difference-between-findandmodify-and-update-in-mongodb ↩︎
https://blog.youkuaiyun.com/salmonellavaccine/article/details/53907535 ↩︎
https://blog.youkuaiyun.com/FS1360472174/article/details/53192296 ↩︎
http://www.runoob.com/mongodb/mongodb-covered-queries.html ↩︎
https://stackoverflow.com/questions/37782251/auto-populate-date-in-mongodb-on-insert ↩︎
https://docs.mongodb.com/manual/reference/operator/update/currentDate/ ↩︎