超实用!手把手入门 MongoDB:这些坑点请一定远离[1]
MONGODB自带SHELL客户端
## 切换到mongo账号
[root@tst01 ~]# su - mongod
## 进入mongod shell客户端
tst01 mongod>mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
Server has startup warnings:
2018-06-02T09:51:06.712+0800 I STORAGE [initandlisten]
2018-06-02T09:51:06.712+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-06-02T09:51:06.712+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-06-02T09:51:06.987+0800 I CONTROL [initandlisten]
2018-06-02T09:51:06.987+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-06-02T09:51:06.987+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-06-02T09:51:06.987+0800 I CONTROL [initandlisten]
2018-06-02T09:51:06.987+0800 I CONTROL [initandlisten]
2018-06-02T09:51:06.987+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
## 显示当前mongod服务器数据库
MongoDB Enterprise > show dbs
admin 0.000GB
foolbar 0.000GB
local 0.000GB
## 查看当前数据库的名称
MongoDB Enterprise > db
test
## 显示当前数据库的所有集合
MongoDB Enterprise > show collections
## 切换到foolbar数据库,如果foolbar数据库不存在,会在该数据第一次插入之后创建
MongoDB Enterprise > use foolbar
switched to db foolbar
## 显示当前数据库中有哪些集合,起初应该是为NULL,但我们在创建mongod服务器时,有测试连接型的时候创建个集合,因此能看到blog集合
MongoDB Enterprise > show collections
blog
## 入数据会创建数据库(集合) 插入的数据是一个文档
MongoDB Enterprise > db.blog.insert({"title":"mongodb shell client"});
WriteResult({ "nInserted" : 1 })
## 查找当前数据库中blog集合的所有文档
MongoDB Enterprise > db.blog.find()
{ "_id" : ObjectId("5b12664097b43b383a56e0c6"), "title1" : "instrduce of mongo" }
{ "_id" : ObjectId("5b12c804c4dbabca2ce3b2a8"), "title" : "mongodb shell client" }
MongoDB Enterprise > db.blog.insert({"docment":"welcome to use mongodb shell client"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.blog.find()
{ "_id" : ObjectId("5b12664097b43b383a56e0c6"), "title1" : "instrduce of mongo" }
{ "_id" : ObjectId("5b12c804c4dbabca2ce3b2a8"), "title" : "mongodb shell client" }
{ "_id" : ObjectId("5b12c838c4dbabca2ce3b2a9"), "docment" : "welcome to use mongodb shell client" }
MongoDB Enterprise > db.blog.insert({"title":"my test","name":"pc"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.blog.insert({"title":"my test","name":"pc"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.blog.insert({"name":"bowenz"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.blog.insert({"name":"bowenz"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.blog.find()
{ "_id" : ObjectId("5b12664097b43b383a56e0c6"), "title1" : "instrduce of mongo" }
{ "_id" : ObjectId("5b12c804c4dbabca2ce3b2a8"), "title" : "mongodb shell client" }
{ "_id" : ObjectId("5b12c838c4dbabca2ce3b2a9"), "docment" : "welcome to use mongodb shell client" }
{ "_id" : ObjectId("5b12c86dc4dbabca2ce3b2aa"), "title" : "my test", "name" : "pc" }
{ "_id" : ObjectId("5b12c873c4dbabca2ce3b2ab"), "title" : "my test", "name" : "pc" }
{ "_id" : ObjectId("5b12c888c4dbabca2ce3b2ac"), "name" : "bowenz" }
{ "_id" : ObjectId("5b12c88bc4dbabca2ce3b2ad"), "name" : "bowenz" }
## 从当前数据库中的blog集合中移除_id为ObjectId("5b12c86dc4dbabca2ce3b2aa")的文档
MongoDB Enterprise > db.blog.remove({_id:ObjectId("5b12c86dc4dbabca2ce3b2aa")})
WriteResult({ "nRemoved" : 1 })
MongoDB Enterprise > db.blog.find()
{ "_id" : ObjectId("5b12664097b43b383a56e0c6"), "title1" : "instrduce of mongo" }
{ "_id" : ObjectId("5b12c804c4dbabca2ce3b2a8"), "title" : "mongodb shell client" }
{ "_id" : ObjectId("5b12c838c4dbabca2ce3b2a9"), "docment" : "welcome to use mongodb shell client" }
{ "_id" : ObjectId("5b12c873c4dbabca2ce3b2ab"), "title" : "my test", "name" : "pc" }
{ "_id" : ObjectId("5b12c888c4dbabca2ce3b2ac"), "name" : "bowenz" }
{ "_id" : ObjectId("5b12c88bc4dbabca2ce3b2ad"), "name" : "bowenz" }
MongoDB Enterprise > show collections
blog
## 在当前数据库创建一个指定名称的集合
MongoDB Enterprise > db.createCollection("course")
{ "ok" : 1 }
MongoDB Enterprise > show collections
blog
course
## 这个shell函数findOne会返回一个文档 而find函数会返回最多二十个文档.更多区别我们在后面详细介绍
MongoDB Enterprise > db.blog.findOne()
{
"_id" : ObjectId("5b12664097b43b383a56e0c6"),
"title1" : "instrduce of mongo"
}
## 统计集合blog所有文档数量
MongoDB Enterprise > db.blog.count()
6
## 退出mongo客户端
MongoDB Enterprise > exit
bye
tst01 mongod>