1. 进入mongo环境
mongo
[root@slave2 ~]# mongo
MongoDB shell version v3.4.16
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.16
Server has startup warnings:
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten]
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten]
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten]
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten]
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-07-20T17:53:38.109+0800 I CONTROL [initandlisten]
>
show dbs;
3.创建或者切换库(
如果数据库不存在,则创建数据库,否则切换到指定数据库)
use mydb;
可以看到,我们刚创建的数据库 mydb并不在数据库的列表中, 要显示它,我们需要向 mydb数据库插入一些数据。
4.创建集合
> db.createCollection("log")
5. 插入数据 (如果插入时集合不存在,则包括创建集合和插入数据两个动作)及更新
> db.logdata.insert({name:'zhangsan', age:'25'});
WriteResult({ "nInserted" : 1 }
> db.logdata.insert({'_id': 'terrywang', 'super_admin': true})
WriteResult({ "nInserted" : 1 })
> db.logdata.find({'_id':'terrywang'})
{ "_id" : "terrywang", "super_admin" : true }
> db.logdata.update({'_id': 'terrywang'}, {$set: {'super_admin': false}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.logdata.find({"_id":"terrywang"})
{ "_id" : "terrywang", "super_admin" : false }
6.查看集合内容
{ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" }
{ "_id" : "terrywang", "super_admin" : false }
> db.logdata.findOne({'_id':'terrywang'})
{ "_id" : "terrywang", "super_admin" : true }
7.按条件筛选
> db.logdata.find({"name":"zhangsan"});
{ "_id" : ObjectId("5b529e14bfde59a50f5e5396"), "name" : "zhangsan", "age" : "25" }
7.1 正则查找
> db.logdata.find({"_id":/e*/i})
{ "_id" : "terrywang", "super_admin" : false }
i表明是否是case-insensitive,有i则表示忽略大小写
7.2 只筛选到某一列
> db.logdata.find({},{D:1})
{ "_id" : ObjectId("5b529ad3aea13d710cc622d5") }
7.3 查询去掉后的当前聚集集合中的某列的重复数据 { "_id" : ObjectId("5b529ad3aea13d710cc622d5"), "name" : "zhangsan", "age" : "25" }
> db.logdata.insert({"name":"Terry","age":"35"})
WriteResult({ "nInserted" : 1 })
> db.logdata.distinct("name")
> db.logdata.insert({"name":"Terry","age":"35"})
WriteResult({ "nInserted" : 1 })
> db.logdata.distinct("name")
会过滤掉name中的相同数据 。相当于:select distict name from logdata;
7.4 查询age = "35"的记录
> db.logdata.find({"age": "35"});
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
相当于 select * from logdata where age = “35”;
7.5 查询age > “26”的记录
> db.logdata.find({age: {$gt: "26"}});
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
7.6 查询age < “26”的记录 > db.logdata.find({age:{$lt:"26"}});
{ "_id" : ObjectId("5b529ad3aea13d710cc622d5"), "name" : "zhangsan", "age" : "25" }
7.6 查询age < “35”的记录
> db.logdata.find({age: {$lte: "35"}});
{ "_id" : ObjectId("5b529ad3aea13d710cc622d5"), "name" : "zhangsan", "age" : "25" }
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
7.7 查询age >= 26并且 age <= 36
> db.logdata.find({age: {$gte: "26", $lte: "35"}});
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
7.8 查询name中包含 Ter的数据
> db.logdata.find({name: /Ter/});
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
7.9 查询name中以Ter开头的
> db.logdata.find({name: /^Ter/});
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
7.10 显示指定列
> db.logdata.find({}, {name: 1, age: 1});
{ "_id" : ObjectId("5b529ad3aea13d710cc622d5"), "name" : "zhangsan", "age" : "25" }
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
> db.logdata.find({}, {age: 1});
{ "_id" : ObjectId("5b529ad3aea13d710cc622d5"), "age" : "25" }
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "age" : "35" }
7.11 按年龄升降序
> db.logdata.find().sort({age: 1})
{ "_id" : ObjectId("5b529ad3aea13d710cc622d5"), "name" : "zhangsan", "age" : "25" }
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
> db.logdata.find().sort({age: -1});
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b529ad3aea13d710cc622d5"), "name" : "zhangsan", "age" : "25" }
7.12 查询前2条
> db.logdata.find().limit(2)
{ "_id" : ObjectId("5b529ad3aea13d710cc622d5"), "name" : "zhangsan", "age" : "25" }
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
7.13 查询1条以后的数据
> db.logdata.find().skip(1)
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
7.14 查询在1-2之间的数据
> db.logdata.find().limit(2).skip(1)
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
7.15 年龄等于“25”或者等于“35”
> db.logdata.find({$or: [{age: "25"}, {age: "35"}]})
{ "_id" : ObjectId("5b529ad3aea13d710cc622d5"), "name" : "zhangsan", "age" : "25" }
{ "_id" : ObjectId("5b52f18c66401eea4b0c151a"), "name" : "Terry", "age" : "35" }
{ "_id" : ObjectId("5b52f1a066401eea4b0c151b"), "name" : "Terry", "age" : "35" }
8.删除文档
先插入一个文档
> db.logdata.insert({name:'lisi', age:'30'});
WriteResult({ "nInserted" : 1 })
{ "_id" : ObjectId("5b529e14bfde59a50f5e5396"), "name" : "zhangsan", "age" : "25" }
{ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" }
删除一个文档
> db.logdata.remove({"name":"zhangsan"})
WriteResult({ "nRemoved" : 1 })
{ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" }
9.显示集合
10.显示集合中文档数
{ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" }
{ "_id" : "terrywang", "super_admin" : false }
11.查看集合索引
2018-07-21T11:42:09.337+0800 E QUERY [thread1] TypeError: db.logdata.getIndex is not a function :
> db.logdata.getIndexes()
12.删除表
11.删除库
进入要删除的库, 执行db.dropDatabase();
{ "dropped" : "test", "ok" : 1 }
12.查看当前的数据库
13. 显示当前db状态
14.显示当前db版本
3.4.16
15. 查看当前db的链接机器地址
connection to 127.0.0.1:27017