MongoDB入门教程(适合小白)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 MongoDB,通用型 2核4GB
简介: 启动和关闭mongodb服务:service mongodb startservice mongodb stop进入shell命令> mongomongodb命令:show dbs:显示数据...

启动和关闭mongodb服务:

service mongodb start
service mongodb stop

进入shell命令

> mongo

mongodb命令:

show dbs:显示数据库列表 
show collections:显示当前数据库中的集合(类似关系数据库中的表tableshow users:显示所有用户 
use yourDB:切换当前数据库至yourDB 
db.help() :显示数据库操作命令 
db.yourCollection.help() :显示集合操作命令,yourCollection是集合名

创建表,插入数据

use school    #创建数据库或使用数据库
db.createCollection('teacher')    #创建Collection
#插入数据(insert与save的区别)
db.teacher.insert({id:1,name:'zhangshan',age:38})
db.teacher.insert({id:2,name:'lisi',age:47})
db.teacher.insert({id:3,name:'wangwu',age:26})

查询数据

db.teacher.count()  #计数统计
db.teacher.find()  #查询所有记录。相当于:select * from teacher
db.teacher.find({name: 'lisi'})  #查询name='lisi'的记录。相当于: select * from teacher where name='lisi'
db.student.find({},{name:1, age:1}) #查询指定列name、age数据。相当于:select name,age from teacher.name:1表示返回name列,默认_id字段也是返回的,可以添加_id:0(意为不返回_id)写成{name: 1, age: 1,_id:0},就不会返回默认的_id字段了
db.teacher.find({name: 'zhangshan', age: 38}) #and 与条件查询。相当于:select * from student where name = 'zhangshan' and age = 38
db.student.find({$or: [{age: 26}, {age: 38}]}) #or 条件查询。相当于:select * from teacher where age = 26 or age = 38
db.teacher.find({},{name:1,age:1,_id:0}).limit(1).skip(1)    #查询第二行的name列和age列,忽略'_id'

排序

> db.teacher.find({},{name:1, age:1, _id:0}).sort({age:-1})
{ "name" : "lisi", "age" : 47 }
{ "name" : "zhangshan", "age" : 38 }
{ "name" : "wangwu", "age" : 26 }
> db.teacher.find({},{name:1, age:1, _id:0}).sort({age:1})
{ "name" : "wangwu", "age" : 26 }
{ "name" : "zhangshan", "age" : 38 }
{ "name" : "lisi", "age" : 47 }

聚合

use school
db.createCollection('article')
db.article.insert([
{
   _id: 100,
   title: 'MongoDB Overview',
   description: 'MongoDB is no sql database',
   by_user: 'Maxsu',
   url: 'http://www.yiibai.com',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
},
{
   _id: 101,
   title: 'NoSQL Overview', 
   description: 'No sql database is very fast',
   by_user: 'Maxsu',
   url: 'http://www.yiibai.com',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 10
},
{
   _id: 102,
   title: 'Neo4j Overview', 
   description: 'Neo4j is no sql database',
   by_user: 'Kuber',
   url: 'http://www.neo4j.com',
   tags: ['neo4j', 'database', 'NoSQL'],
   likes: 750
},
{
   _id: 103,
   title: 'MySQL Overview', 
   description: 'MySQL is sql database',
   by_user: 'Curry',
   url: 'http://www.yiibai.com/mysql/',
   tags: ['MySQL', 'database', 'SQL'],
   likes: 350
}])
*************************************************
#count(*)
> db.article.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
{ "_id" : "Curry", "num_tutorial" : 1 }
{ "_id" : "Kuber", "num_tutorial" : 1 }
{ "_id" : "Maxsu", "num_tutorial" : 2 }
*************************************************
#avg
> db.article.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])
{ "_id" : "Curry", "num_tutorial" : 350 }
{ "_id" : "Kuber", "num_tutorial" : 750 }
{ "_id" : "Maxsu", "num_tutorial" : 55 }
*************************************************
#max
> db.article.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
{ "_id" : "Curry", "num_tutorial" : 350 }
{ "_id" : "Kuber", "num_tutorial" : 750 }
{ "_id" : "Maxsu", "num_tutorial" : 100 }

*************************************************
#min
> db.article.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
{ "_id" : "Curry", "num_tutorial" : 350 }
{ "_id" : "Kuber", "num_tutorial" : 750 }
{ "_id" : "Maxsu", "num_tutorial" : 10 }
*************************************************
#sum
> db.article.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
{ "_id" : "Curry", "num_tutorial" : 350 }
{ "_id" : "Kuber", "num_tutorial" : 750 }
{ "_id" : "Maxsu", "num_tutorial" : 110 }
*************************************************

修改数据
db.youCollection.update(criteria, objNew, upsert, multi )
criteria: update的查询条件,类似sql update查询内where后面的
objNew : update的对象和一些更新的操作符(如$set)等,也可以理解为sql update查询内set后面的。
upsert : 如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi: mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。默认false,只修改匹配到的第一条数据。
其中criteria和objNew是必选参数,upsert和multi可选参数


db.teacher.update({name: 'lisi'}, {$set: {age: 30}}, false, true) #相当于:update teacher set age =30 where name = 'lisi';

删除数据

db.teacher.remove({name: 'wangwu'}) #相当于:delete from teacher where name='wangwu'

删除集合

db.teacher.drop()
show collections

删除数据库

> use school
> db.dropDatabase()
{ "dropped" : "school", "ok" : 1 }

退出shell命令行模式

exit或者Ctrl+C

mongodb可视化工具:robomongo, 软件名称:robo3t.
启动robo3t:

cd ~
robo3t-1.1.1-linux-x86_64-c93c6b0/bin/robo3t  #robo3t所在目录

利用Python操作mongodb

from pymongo import MongoClient

conn = MongoClient('mongodb://localhost:27017/')  #连接mongodb
db = conn.testdb  #使用testdb数据库
#在teacher表中插入数据
db.teacher.insert([
 {"name":'zhanshan','age':25},
 {"name":'lisi','age':36},
 {"name":'wangwu','age':27},
])
print("OK!")  #提示完成
相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
5月前
|
开发框架 NoSQL JavaScript
mongoDB入门教程四:安装Node+express环境支撑
mongoDB入门教程四:安装Node+express环境支撑
48 0
|
5月前
|
NoSQL 数据可视化 关系型数据库
mongoDB入门教程三:数据库的创建和删除(增删改查)基本命令
mongoDB入门教程三:数据库的创建和删除(增删改查)基本命令
130 0
|
5月前
|
NoSQL MongoDB 数据库
mongoDB入门教程一:下载安装和环境配置、连接运行
mongoDB入门教程一:下载安装和环境配置、连接运行
200 0
|
5月前
|
NoSQL 数据可视化 MongoDB
mongoDB入门教程二:推荐一款好用的mongoDB可视化工具Robo 3T
mongoDB入门教程二:推荐一款好用的mongoDB可视化工具Robo 3T
237 1
mongoDB入门教程二:推荐一款好用的mongoDB可视化工具Robo 3T
|
5月前
|
NoSQL 前端开发 MongoDB
mongoDB入门教程五:搭建一个简单的登陆注册界面
mongoDB入门教程五:搭建一个简单的登陆注册界面
106 0
|
6月前
|
SQL NoSQL MongoDB
软件开发入门教程网之Python MongoDB
软件开发入门教程网之Python MongoDB
45 0
|
6月前
|
SQL NoSQL MongoDB
软件开发入门教程网之Python MongoDB
软件开发入门教程网之Python MongoDB
|
7月前
|
NoSQL JavaScript Java
MongoDB 入门教程系列之三:使用 Restful API 操作 MongoDB
MongoDB 入门教程系列之三:使用 Restful API 操作 MongoDB
77 0
|
7月前
|
存储 NoSQL Java
MongoDB 入门教程系列之二:使用 Spring Boot 操作 MongoDB
MongoDB 入门教程系列之二:使用 Spring Boot 操作 MongoDB
98 0
|
11月前
|
存储 NoSQL MongoDB
软件开发入门教程网之MongoDB 数据库引用
【摘要】 本章将会讲解考虑这样的一个场景,我们在不同的集合中 (address_home, address_office, address_mailing, 等)存储不同的地址(住址,办公室地址,邮件地址等)。 这样,我们在调用不同地址时,也需要指定集合,一个文档从多个集合引用文档,我们应该使用 DBRefs。