惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
C
Check Point Blog
博客园_首页
B
Blog
D
Docker
U
Unit 42
量子位
I
InfoQ
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
美团技术团队
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Google DeepMind News
Google DeepMind News

博客园 - 王晓成

Spark RDD 操作 (转)Mysql哪些字段适合建立索引 Specified key was too long; max key length is 767 bytes解决方案 (转)并发编程 – Concurrent 用户指南 协同过滤 (转)K-近邻算法(KNN) 贝叶斯、朴素贝叶斯及调用spark官网 mllib NavieBayes示例 决策树之ID3,C4.5及CART kmeans Spark下的FP-Growth和Apriori scala spark-streaming整合kafka (spark 2.3 kafka 0.10) (转)Java 详解 JVM 工作原理和流程 Scala map与flatMap php 正则表达式 (转发)storm 入门原理介绍 shell :将标准输出及标准错误输出写到指定文件 shell循环(两个日期比较,改变某个特定日期来改变当前比较值) (转)cenntos 安装mongodb 通过spark sql 将 hdfs上文件导入到mongodb
MongoDB基本操作
王晓成 · 2018-07-21 · via 博客园 - 王晓成

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] 

>

2.查询所有数据库

show dbs;  

> show dbs 

admin  0.000GB

local  0.000GB 

3.创建或者切换库如果数据库不存在,则创建数据库,否则切换到指定数据库

use mydb; 

> use mydb

switched to db mydb

> show dbs

admin  0.000GB

local  0.000GB

可以看到,我们刚创建的数据库 mydb并不在数据库的列表中, 要显示它,我们需要向 mydb数据库插入一些数据。

4.创建集合 

> db.createCollection("log")

{ "ok" : 1 }

> show dbs

admin  0.000GB

local  0.000GB

mydb   0.000GB

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.查看集合内容

> db.logdata.find()

{ "_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 查询去掉后的当前聚集集合中的某列的重复数据

> db.logdata.find()

{ "_id" : ObjectId("5b529ad3aea13d710cc622d5"), "name" : "zhangsan", "age" : "25" }

> db.logdata.insert({"name":"Terry","age":"35"})

WriteResult({ "nInserted" : 1 })

> db.logdata.distinct("name")

[ "zhangsan", "Terry" ]

> db.logdata.insert({"name":"Terry","age":"35"})

WriteResult({ "nInserted" : 1 })

> db.logdata.distinct("name")

[ "zhangsan", "Terry" ]

 会过滤掉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 })

> db.logdata.find()

{ "_id" : ObjectId("5b529e14bfde59a50f5e5396"), "name" : "zhangsan", "age" : "25" }

{ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" } 

删除一个文档

> db.logdata.remove({"name":"zhangsan"}) 

WriteResult({ "nRemoved" : 1 })

> db.logdata.find()

{ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" }

 9.显示集合 

> show tables;

log

logdata

10.显示集合中文档数

> db.logdata.find()

{ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" }

{ "_id" : "terrywang", "super_admin" : false }

> db.logdata.count()

2

11.查看集合索引

> db.logdata.getIndex()

2018-07-21T11:42:09.337+0800 E QUERY    [thread1] TypeError: db.logdata.getIndex is not a function :

@(shell):1:1

> db.logdata.getIndexes()

[

        {

                "v" : 2,

                "key" : {

                        "_id" : 1

                },

                "name" : "_id_",

                "ns" : "mydb.logdata"

        }

12.删除表 

> db.logdata.drop() 

true

> show tables;

log

test

> db.test.drop()

true

> show tables

log

11.删除库

进入要删除的库, 执行db.dropDatabase();

> show dbs;

admin  0.000GB

local  0.000GB

mydb   0.000GB

mydb2  0.000GB

test   0.000GB

> use test;

switched to db test

> db.dropDatabase();

{ "dropped" : "test", "ok" : 1 }

> show dbs;

admin  0.000GB

local  0.000GB

mydb   0.000GB

mydb2  0.000GB

12.查看当前的数据库 

> use mydb; 

switched to db mydb

> db.getName()

mydb

 13. 显示当前db状态 

> db.stats() 

{

        "db" : "mydb",

        "collections" : 2,

        "views" : 0,

        "objects" : 1,

        "avgObjSize" : 53,

        "dataSize" : 53,

        "storageSize" : 20480,

        "numExtents" : 0,

        "indexes" : 2,

        "indexSize" : 20480,

        "ok" : 1

}

14.显示当前db版本 

> db.version() 

3.4.16

 15. 查看当前db的链接机器地址

> db.getMongo()

connection to 127.0.0.1:27017