mongoDB使用方法

进入数据库:use  数据库

创建集合(表):db.createCollection("mycollection")-----------------------------------------------------------create table mycollection(...)

插入数据:db.mycollection.insert({name:"xxx",sex:1,age:22.........})-----------------------------------insert  mycollection value("xxx")

查询所有:db.mycollection.find()--------------------------------------------------------------------------------select  * from mycollection

条件查询:db.mycollection.find({"name":"xxx"})-------------------------------------------------------------select * from mycollection where name="xxx"

and条件查询:db.mycollection.find({"name":"xxx","sex":1})------注:条件之间用逗号隔开---------select * from mycollection where name="xxx" and sex=1

or条件查询:db.mycollection.find({$or:["name":"xxx"},{"sex":1]})------注意括号和$or关键字-----select * from mycollection where name="xxx" or sex=1

in查询:db.mycollection.find({"name":{$in:["xxx","xx"]}})------注意括号和$in关键字----------------select * from mycollection where name in("xxx","xx")

notin查询:db.mycollection.find({"name":{$nin:["xxx","xx"]}})------注意括号和$nin关键字---------select * from mycollection where name notin("xxx","xx")

order by查询:db.mycollection.find().sort({key:-1})------sort方法-1倒叙1升序------------------------select * from mycollection order by id desc

求平均值:.db.mycollection.aggregate([{$group:{_id:"$id","avg":{$avg:"$age"}}}])---------------------select avg(age) from mycollection group by id 

求数量:.db.mycollection.aggregate([{$group:{_id:"$id","sum":{$sum:"$age"}}}])---------------------select count(*) from mycollection group by id

修改数据: db..mycollection.update({"name":"xxx"},{$set:{"name":"xx"}})--------------------------------update mycollection set name='xx' where name="xxx"

删除数据:db.mycollection.remove({"name":"xxx"}) -------------------------------------------------------delete mycollection where name="xxx"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值