mongodb概念:
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立[索引]。
SQL和NoSQL的主要区别
- 在SQL中层级关系:数据库->表->数据
- 在NoSQL中是:数据库->集合->数据
MongoDB的优势
1.无数据结构限制
• 没有表结构的概念,每条记录可以有完全不同的结构
• 业务开发方便快捷
{name:'小明',sex:'男'}
{name:'jerry',address:'东北'}
{name:'小红',home:[{'山东'},{江西}]}
2.大数据量和高性能
• nosql数据库都具有非常高的读写性能,尤其在大数量下表现优秀
3.良好的支持
• 完善的文档
• 齐全的驱动支持
mongodb的安装
- 下载网址:https://www.mongodb.com/download-center#community
本人操作系统win2012,所以选择4.2适配版本 - 注意:
安装时前面只要点击下一步都没问题,接下来的一步千万要小心,之前就是一个不留神麻溜的点击了“next”,结果苦等了一个小时也没结束。重要的事情说三遍,“取消”、“取消”、“取消勾选左侧的Install MongoDB Copmpass”选项。 - 把bin目录添加到path环境变量里面 (C:\Program Files\MongoDB\Server\4.2\bin)
- 启动MongoDB
mongod --dbpath C:\Program Files\MongoDB\Server\4.2\data
链接 mongo
mongodb基本命令
- cls 清屏
- show dbs 查询所有数据库
- use 数据库名称 打开或新建数据库
- show tables 查询该数据库下的所有集合
- db.表名.find()查询该表下的所有记录
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
name 0.000GB
> use name
switched to db name
> show tables
jerry
jerry2
> db.jerry2.find()
{ "_id" : ObjectId("60d1e5d6eea5b4f856a79e43"), "name" : "jerry", "age" : 16, "gengder" : "true", "home" : "chang
{ "_id" : ObjectId("60d1e64feea5b4f856a79e44"), "name" : "amy", "age" : 18, "gengder" : "false", "home" : "shangh
{ "_id" : ObjectId("60d1e6edeea5b4f856a79e45"), "name" : "jack", "age" : 18, "gengder" : "true", "home" : "wuhan"
{ "_id" : ObjectId("60d1e7a4eea5b4f856a79e46"), "name" : "tomy", "age" : "30", "gengder" : "false", "home" : "put
{ "_id" : ObjectId("60d1e84ceea5b4f856a79e47"), "name" : "rose", "age" : 45, "gengder" : "true", "home" : "fuzhou
>
mongodb插入数据
- 一条条插入,注意要用[ ]。
db.jerry2.insert({'name':'jerry','age':16,'gengder':'true','home':'changsai'}) - 批量插入
db.stu.insert([{"name" : "张三", "hometown" : "长沙", "age" : 20, "gender" : true },
{"name" : "老李", "hometown" : "广州", "age" : 18, "gender" : false },
{"name" : "王麻子", "hometown" : "北京", "age" : 18, "gender" : false },
{"name" : "刘六", "hometown" : "深圳", "age" : 40, "gender" : true },
{"name" : "jerry", "hometown" : "长沙", "age" : 16, "gender" : true },
{"name" : "小永", "hometown" : "广州", "age" : 45, "gender" : true },
{"name" : "老amy", "hometown" : "衡阳", "age" : 18, "gender" : true }])
mongodb查询数据
db.stu.find() 查询数据/可以在括号里面精确查找
db.stu.find({name:'jerry'}).pretty()
db.stu.find({age:18})
db.stu.findOne({age:18}) 把满足结果的第一条数据返回
db.stu.find({age:18,hometown:'衡阳'}) 多个条件查找
db.stu.find({age:{gte:18}}) 年龄大于等于18
db.stu.find({age:{or:[{age:{where:function(){return this.age>18}})定义方法查询 年龄大于18
> use name
switched to db name
> db.jerry2.find()
{ "_id" : ObjectId("60d1e5d6eea5b4f856a79e43"), "name" : "jerry", "age" : 16, "gengder" : "true", "home" : "changsai" }
{ "_id" : ObjectId("60d1e64feea5b4f856a79e44"), "name" : "amy", "age" : 18, "gengder" : "false", "home" : "shanghai" }
{ "_id" : ObjectId("60d1e6edeea5b4f856a79e45"), "name" : "jack", "age" : 18, "gengder" : "true", "home" : "wuhan" }
{ "_id" : ObjectId("60d1e7a4eea5b4f856a79e46"), "name" : "tomy", "age" : "30", "gengder" : "false", "home" : "putian" }
{ "_id" : ObjectId("60d1e84ceea5b4f856a79e47"), "name" : "rose", "age" : 45, "gengder" : "true", "home" : "fuzhou" }
> db.jerry2.find({'name':'amy'})
{ "_id" : ObjectId("60d1e64feea5b4f856a79e44"), "name" : "amy", "age" : 18, "gengder" : "false", "home" : "shanghai" }
> db.jerry2.find({'age':18}).pretty
function() {
this._prettyShell = true;
return this;
}
> db.jerry2.find({'name':'jerry'}).pretty
function() {
this._prettyShell = true;
return this;
}
> db.jerry2.find({name:'jerry'}).pretty
function() {
this._prettyShell = true;
return this;
}
> db.jerry2.find().pretty()
{
"_id" : ObjectId("60d1e5d6eea5b4f856a79e43"),
"name" : "jerry",
"age" : 16,
"gengder" : "true",
"home" : "changsai"
}
{
"_id" : ObjectId("60d1e64feea5b4f856a79e44"),
"name" : "amy",
"age" : 18,
"gengder" : "false",
"home" : "shanghai"
}
{
"_id" : ObjectId("60d1e6edeea5b4f856a79e45"),
"name" : "jack",
"age" : 18,
"gengder" : "true",
"home" : "wuhan"
}
{
"_id" : ObjectId("60d1e7a4eea5b4f856a79e46"),
"name" : "tomy",
"age" : "30",
"gengder" : "false",
"home" : "putian"
}
{
"_id" : ObjectId("60d1e84ceea5b4f856a79e47"),
"name" : "rose",
"age" : 45,
"gengder" : "true",
"home" : "fuzhou"
}
> db.jerry2.find({'name':'jerry'}).pretty()
{
"_id" : ObjectId("60d1e5d6eea5b4f856a79e43"),
"name" : "jerry",
"age" : 16,
"gengder" : "true",
"home" : "changsai"
}
> db.jerry2.find({'name':'amy','home':'shanghai'})
{ "_id" : ObjectId("60d1e64feea5b4f856a79e44"), "name" : "amy", "age" : 18, "gengder" : "false", "home" : "shanghai" }
> db.jerry2.find({'name':'amy','home':'shanghai'}).pretty()
{
"_id" : ObjectId("60d1e64feea5b4f856a79e44"),
"name" : "amy",
"age" : 18,
"gengder" : "false",
"home" : "shanghai"
}
> db.jerry2.find({'age':{$gt:16}}.pretty()
> db.jerry2.find({'age':{$gt:16}}).pretty()
{
"_id" : ObjectId("60d1e64feea5b4f856a79e44"),
"name" : "amy",
"age" : 18,
"gengder" : "false",
"home" : "shanghai"
}
{
"_id" : ObjectId("60d1e6edeea5b4f856a79e45"),
"name" : "jack",
"age" : 18,
"gengder" : "true",
"home" : "wuhan"
}
{
"_id" : ObjectId("60d1e84ceea5b4f856a79e47"),
"name" : "rose",
"age" : 45,
"gengder" : "true",
"home" : "fuzhou"
}
> db.jerry2.find({'age':{$gte:18},'name':'amy'}).pretty()
{
"_id" : ObjectId("60d1e64feea5b4f856a79e44"),
"name" : "amy",
"age" : 18,
"gengder" : "false",
"home" : "shanghai"
}
mongodb操作查询结果
db.stu.find({age:18}).count() 返回查询结果的数量
db.stu.find().skip(2).limit(2) 先跳过2条数据 在返回满足结果的前2条数据
映射
指定返回的字段
如果为1返回该字段 映射主key(id) 需要_id:0
排序
db.stu.find().sort({age:1}) 按照年龄升序
db.stu.find().sort({age:-1}) 按照年龄降序
> db.jerry2.find().limit(3)
{ "_id" : ObjectId("60d1e5d6eea5b4f856a79e43"), "name" : "jerry", "age" : 16, "gengder" : "true", "home" : "changsai" }
{ "_id" : ObjectId("60d1e64feea5b4f856a79e44"), "name" : "amy", "age" : 18, "gengder" : "false", "home" : "shanghai" }
{ "_id" : ObjectId("60d1e6edeea5b4f856a79e45"), "name" : "jack", "age" : 18, "gengder" : "true", "home" : "wuhan" }
> db.jerry2.find().skip(2).limit()
{ "_id" : ObjectId("60d1e6edeea5b4f856a79e45"), "name" : "jack", "age" : 18, "gengder" : "true", "home" : "wuhan" }
{ "_id" : ObjectId("60d1e7a4eea5b4f856a79e46"), "name" : "tomy", "age" : "30", "gengder" : "false", "home" : "putian" }
{ "_id" : ObjectId("60d1e84ceea5b4f856a79e47"), "name" : "rose", "age" : 45, "gengder" : "true", "home" : "fuzhou" }
> db.jerry2.find().count()
5
> db.jerry2.find({})
{ "_id" : ObjectId("60d1e5d6eea5b4f856a79e43"), "name" : "jerry", "age" : 16, "gengder" : "true", "home" : "changsai" }
{ "_id" : ObjectId("60d1e64feea5b4f856a79e44"), "name" : "amy", "age" : 18, "gengder" : "false", "home" : "shanghai" }
{ "_id" : ObjectId("60d1e6edeea5b4f856a79e45"), "name" : "jack", "age" : 18, "gengder" : "true", "home" : "wuhan" }
{ "_id" : ObjectId("60d1e7a4eea5b4f856a79e46"), "name" : "tomy", "age" : "30", "gengder" : "false", "home" : "putian" }
{ "_id" : ObjectId("60d1e84ceea5b4f856a79e47"), "name" : "rose", "age" : 45, "gengder" : "true", "home" : "fuzhou" }
> db.jerry2.find({},{age:1,_id:0})
{ "age" : 16 }
{ "age" : 18 }
{ "age" : 18 }
{ "age" : "30" }
{ "age" : 45 }
> db.jerry2.find({},{age:-1,_id:0})
{ "age" : 16 }
{ "age" : 18 }
{ "age" : 18 }
{ "age" : "30" }
{ "age" : 45 }
> db.jerry2.find({'age':{$gt:18}},{age:1,id:0})
Error: error: {
"ok" : 0,
"errmsg" : "Projection cannot have a mix of inclusion and exclusion.",
"code" : 2,
"codeName" : "BadValue"
}
> db.jerry2.find({'age':{$gt:18}},{age:1,_id:0})
{ "age" : 45 }
> db.jerry2.find().sort({age:1})
{ "_id" : ObjectId("60d1e5d6eea5b4f856a79e43"), "name" : "jerry", "age" : 16, "gengder" : "true", "home" : "changsai" }
{ "_id" : ObjectId("60d1e64feea5b4f856a79e44"), "name" : "amy", "age" : 18, "gengder" : "false", "home" : "shanghai" }
{ "_id" : ObjectId("60d1e6edeea5b4f856a79e45"), "name" : "jack", "age" : 18, "gengder" : "true", "home" : "wuhan" }
{ "_id" : ObjectId("60d1e84ceea5b4f856a79e47"), "name" : "rose", "age" : 45, "gengder" : "true", "home" : "fuzhou" }
{ "_id" : ObjectId("60d1e7a4eea5b4f856a79e46"), "name" : "tomy", "age" : "30", "gengder" : "false", "home" : "putian" }
>