2.5 Mongodb Shell (javescript标准)
运行shell:
[root@instance-795y2mib oradata]# mongo
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("59065313-0a2f-4965-a6b2-b63ba47444fa") }
MongoDB server version: 4.0.6
Welcome to the MongoDB shell.
..
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
mongodb客户端:
默认连到test数据库,赋值个db变量
> db
test
1. 创建
> post = {"a": 1,
... "b":"hahaha",
... "c": new Date()}
{ "a" : 1, "b" : "hahaha", "c" : ISODate("2019-02-24T07:35:11.339Z") }
> db.blog.insert(post);
WriteResult({ "nInserted" : 1 })
> db.blog.find()
{ "_id" : ObjectId("5c7249431ac71327eeef7a64"), "a" : 1, "b" : "hahaha", "c" : ISODate("2019-02-24T07:35:11.339Z") }
2. 读取
find() --显示20条记录
findOne() --返回一个?
3.更新
update至少两个参数。
--赋值新集合,新文档
> temp.d = "insert"
insert
> temp.b = "update_ok"
update_ok
> db.blog.update({"a" : 1},temp)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.find();
{ "_id" : ObjectId("5c724cf51ac71327eeef7a67"), "d" : "insert", "b" : "update_ok" }
4. 删除
db.blog.drop({"d" : "insert"})
2.6.4嵌套文档
me = { "name": "tangyi",
"age":30,
"address": {
"city":"guangzhou",
"road":"longjidong"
}
}
> db.me.insert(me)
WriteResult({ "nInserted" : 1 })
2.6.5 _id和ObjectId
文档中必须有一个_id,默认是objectid
objectid:12字节,24个十六进制
一秒钟允许每个进程拥有最多2563个不同objectid
手工创建_id
> db.blog.insert({_id:1,"a":"123zs"})
> db.blog.find({"_id":1})
{ "_id" : 1, "a" : "123zs" }
2.7 客户端连接到其他DB
or
mongo --nodb
2.7.2 执行shell脚本
mongo scripts.js script2.js
--指定主机、端口、执行脚本
mongo --quiet host1:30000/foo script1.js script2.js
--load函数执行
>load("scripts1.js")
2.7.3 创建.mongorc.js 文件
(类似oracle的.login.sql 文件) shell打开会自动跑
缩写别名,屏蔽危险命令
启动加入--norc,会禁止加载.mongorc.js文件
2.7.4 定制shell提示
2.7.6 命名