一、基础命令
use test–选库
显示 switched to db test
db.createCollection(‘goods’)
显示(‘ok’:1)
show dbs
显示当前所有的库
use shop
switched to db shop
db.goods.insert({name:‘zhangsan’,age:9})
显示writeResult({‘nInserted’:1})
show dbs–显示当前所有的库
2,查看数据库下面的集合
use shop
switched to db shop
show collections
goods
show tables
goods
3,删除数据和删除集合
use test
switched to db test
db.dropDatabase()
(“dropped”:“test”,“ok”:1)
use shop
switched to db shop
db.goods.drop()
true
二、CRUD命令
1、插入:insert
单条插入
db.stu.insert({name:‘lisi’,age:10,gender:1})
显示 writeResult({“nInserted”:1})–成功 0则失败 _id字段自动创建,主键自动设置到这个字段 这个字段可手动指定,但不可重复
db.stu.find()查看
显示 _id:Obj(一堆乱码),name:lisi,age:10,gender:1
多条插入
db.stu.insert({文档},{文档},{文档}…)
查找 find
语法:db.stu.find(查询表达式,查询的列)
db.stu.find({},{name:1,_id:0})—0不显示 1显示
查询表达式:条件过滤
Sql
select*from stu
Where 查询表达式
关系:
(1)等于:
Key=value–>{name:‘lisi’}–{key:value}
查询年龄为11的人
db.stu.find({age:11},{age:1,_id:0,name:1})
(2)不等关系
大于:>–KaTeX parse error: Expected '}', got 'EOF' at end of input: gt-->{key:{gt:value}}
年龄大于20
db.stu.find({age:{KaTeX parse error: Expected 'EOF', got '}' at position 6: gt:20}̲},{_id:0,name:1…gte–>{KaTeX parse error: Expected 'EOF', got '}' at position 10: gte:value}̲ 小于:<---->lt—>{KaTeX parse error: Expected 'EOF', got '}' at position 9: lt:value}̲ 年龄小于20岁的 d…lt:20}},{_id:0,name:1,age:1})
小于等于:<=—KaTeX parse error: Expected '}', got 'EOF' at end of input: lte-->key:{lte:value}
不等于:<>—KaTeX parse error: Expected '}', got 'EOF' at end of input: ne--->key:{ne:value}
在条件查询中的and,多个条件
Where age=10 and name=–>{age:10,name:‘lisi’}
这种and操作不能作范围查询,只能做等值查询
db.stu.find(age:{KaTeX parse error: Expected 'EOF', got '}' at position 6: gt:10}̲,age:{lt:100},{_id:0})
KaTeX parse error: Expected '}', got 'EOF' at end of input: …stu.find({age:{nin:[14,15,8]}},{_id":0})
KaTeX parse error: Expected '}', got 'EOF' at end of input: …u.find({bobby:{all:[‘basketball’,‘football’]}},{_id:0})
KaTeX parse error: Expected '}', got 'EOF' at end of input: …u.find({bobby:{exists:0},{_id:0})
查询练习:
//主键为32的商品
db.goods.find({goods_id:32},{_id:0})
//不属于第3栏目的所有商品{KaTeX parse error: Expected 'EOF', got '}' at position 3: ne}̲ db.goods.find(…ne:3}},{_id:0,cat_id:1})
本店价格高于3000元的商品{KaTeX parse error: Expected 'EOF', got '}' at position 3: gt}̲ db.goods.find(…gt:3000}},{_id:0,shop_price:1})
//本店价格低于或等于100元的商品{KaTeX parse error: Expected 'EOF', got '}' at position 4: lte}̲ db.goods.find(…lte:100}},{_id:0,shop_price:1})
取出第4栏目或第11栏目的商品
db.goods.find({cat_id:{KaTeX parse error: Expected 'EOF', got '}' at position 10: in:{4,11}}̲},{_id:0,cat_id…and}
db.库名.find({KaTeX parse error: Expected 'EOF', got '}' at position 18: …d:[{条件1},{条件2}]}̲,{...}]}) 所有条…and:{shop:{KaTeX parse error: Expected 'EOF', got '}' at position 8: gte:100}̲},{lte:500}},{_id:0,shop_price:1})
取出不属于第3栏目且不属于第11栏目的商品($and nor分别实现)
#用and实现
db.goods.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: and:[{cat_id::{ne:11}}]},{_id:0,cat_id:1})
#用nin实现
db.goods.find({cat_id:{KaTeX parse error: Expected 'EOF', got '}' at position 11: nin:{3,11}}̲},{_id:0,cat_id…nor实现
{KaTeX parse error: Expected 'EOF', got '}' at position 22: …件1],[条件2],{...}}̲] 表示所有条件都不满足…nor:[{cat_id:3},{cat_id:11}]},{_id:0,cat_id:1})
//取出价格大于100且小于300或者大于4000且小于5000的商品()
{KaTeX parse error: Expected 'EOF', got '}' at position 23: …1},{条件2},{...}]}̲ db.goods.find(…or:[{KaTeX parse error: Expected '}', got 'EOF' at end of input: …:[{shop_price:{lt:500}},{KaTeX parse error: Expected '}', got 'EOF' at end of input: …:[{shop_price:{gt:4000}},{shop_price:{KaTeX parse error: Expected 'EOF', got '}' at position 8: lt:5000}̲}]}]}]},{_id:0,…mod:[5,1]}},{goods_id:1,_id:0})
查询结果的筛选函数
limit(3)从查询结果筛选前三条
db.goods.find().limit(3)
Skip(3)从查询结果中跳过前3天的,显示其他的
db.goods.find().skip(28)
Count():用于统计查询结果的条数
db.goods.find().count()
Sort({key:1/-1}):按照指定的key这一列进行排序,-1是逆序
db.goods.find({},{_id:0,goods_number:1}).sort({goods_number:1})
db.goods.find({},{_id:0,goods_number:1}).sort({goods_number:-1})
2、删除:remove
db.goods.remove(查询表达式,选项)
选项:{justOne:true}:只删除一条,默认是falase,全部删除。
db.goods.remove({goods_id:{$lt:5}},{justOne:true})
3。更新update
新文档替换旧文档
db.stu.update({name:‘lisi’},{age:1000})
如果想要修改某些列,必须配合更新表达式来操作。
KaTeX parse error: Expected '}', got 'EOF' at end of input: …{name:'lisi'},{set:{age:2}})
db.stu.find({name:‘lisi’})
显示 _id:一堆乱码,name:‘lisi’,age:2 skill:feidao
KaTeX parse error: Expected '}', got 'EOF' at end of input: …{name:'lisi'},{unset:{bobby:1}})
db.stu.update({name:‘peien2’},{KaTeX parse error: Expected 'EOF', got '}' at position 16: unset:{skill:0}}̲) db.stu.find({…rename 重命名某个列
db.stu.update({age:13},{KaTeX parse error: Expected 'EOF', got '}' at position 25: …ame:'new_name'}}̲) db.stu.find({…inc 增长某个列
db.stu.update({age:10},{$inc:{age:1}},{multi:true})
选项:{multi:true}:更新多个,默认为false只更新一个条
>db.stu.update({age:10},{KaTeX parse error: Expected 'EOF', got '}' at position 12: inc:{age:1}}̲,{multi:true})
…set:{goods_name:“oppo原装耳机”}})
db.goods.find({goods_id:3})
Goods_id:2,goods_name:华为手机,shop_price:1000
>db.goods.update({goods_id:2},{$set:{goods_name:‘华为手机’,shop_price:1000}},{upsert:true})
>db.goods.find({goods_id:2})
三、聚合操作
查询每个栏目下的商品数量
db.goods.aggregate({KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:'cat_id’,total:{KaTeX parse error: Expected 'EOF', got '}' at position 6: sum:1}̲}}) .查询goods…group:{_id:null,total1:{$sum:1}}}])
查询每个栏目下价格大于50元的商品个数
>db.goods.aggregate([{$match:{shop_price:{$gt:50}}},{$group:{_id:'$cat_id',total1:{5$sum:1}}}])
查询每个栏目下的库存量的平均值
>db.goods.aggregate([{$group:{_id:'$cat_id',avg:{$avg:'$goods_number'}}}])
查询每个栏目下的库存量的最大值
>db.goods.aggregate([{_id:'$cat_id',max:{$max:'$goods_number'}}])
查询每个栏目下的库存量的最小值
>db.goods.aggregate([{_id:'$cat_id',min:{$max:'$goods_number'}}])
查询每个栏目下的库存量
>db.goods.aggregate()[{$group:{_id:'$cat_id',sum:{$sum:'$goods_number'}}}]
查询每个栏目下 价格大于50元的商品个数 #并筛选出"满足条件的商品个数" 大于等于3的栏目
{大于50}
>db.goods.aggregate([{$match:{shop_price:{$gt:50}}},{$group:{_id:'cat_id',t:{$sum:1}}},{$match:{$gt:3}}])
游标的forEach:
>var update_age=function(obj){
db.aaa.update({age:obj.age},{$set:{age:0}}})
>c.forEach(update_age)
forEach函数就是讲游标中的每一条数据,出入update_age方法中,就是这个obj参数,可以对每一条游标中的文档做一个批量操作。
三、索引
创建索引:
db.goods.ensureIndex({cat_id:-1})
查看集合中所有索引
db.goods.getIndexes()
删除索引:
db.goods.dropIndex(goods_id:-1)
-1和1就是索引的顺序,必须设置原来正确顺序才能删除成功
删除所有索引:除了_id
db.goods.dropIndex()
db.goods.getIndexes()
创建多列索引:
db.collection.ensureIndex({field1:1/-1,field2:1/-1})
db.goods.ensureIndex({goods_id:1,shop_price:1})
db.goods.getIndexes()
子文档索引
db.phone.insert({name:‘oppo’,SPC:{weight:‘20kg’,area:‘bj’}})
db.goods.find({shop_price:{$gt:3000}},{_id:0,shop_price:1})
db.phone.insert({name:‘apple’,SPC:{weight:‘22kg’,area:‘usa’}})
db.phone.find({‘SPC.area’:‘bj’})
db.phone.ensureIndex({‘SPC’,‘area’:1})
唯一索引:{unique:true}:不能重复插入
db.goods.ensureIndex({goods_id:1},{unique:true})
db.goods.insert({goods_id:5})
导出:
mongoexport -d shop -c goods -f goods_id,goods_name,shop_price,goods_number --type csv -o goods.csv
mongoexport -d shop -c goods -o goods.json
导入:
mongoimport -d shop -c goods --file goods.json
配置复制集
1、打开三个cmd,输入三条命令,开启三个服务器
mongod --dbpath C:\MongoDB\Server\3.4\data\db1 --logpath C:\MongoDB\Server\3.4\data\logs\mongo1.log --port 27017 --replSet rs
mongoimport -d shop -c goods --file goods_json
mongod --dbpath C:\MongoDB\Server\3.4\data\db2 --logpath C:\MongoDB\Server\3.4\data\logs\mongo2.log --port 27018 --replSet rs
mongod --dbpath db1路径 --logpath log路径\你创的日志名.log --port 27018 --replSet rs
mongod --dbpath C:\MongoDB\Server\3.4\data\db3 --logpath C:\MongoDB\Server\3.4\data\logs\mongo3.log --port 27019 --replSet rs
>mongod --dbpath db2路径 --logpath log路径\你创的日志名.log --port 27018 --replSet rs
2、再打开一个cmd,进入27017的数据库下面做配置
mongo
var rsconf = {
_id:'rs',
members:[
{_id:0,host:'127.0.0.1:27017'},
{_id:1,host:'127.0.0.1:27018'},
{_id:2,host:'127.0.0.1:27019'}]
}
3、初始化配置
rs.initiate(rsconf)
4、做验证:
在主机创建一个数据库,插入数据,在从机中查看,如果能找到,说明配置成功。
在从机中验证
想要在从机中查看数据,必须输入:
验证27019
来源:CSDN
作者:return_min
链接:https://blog.csdn.net/return_min/article/details/103755567