mongoDB使用方法

来源:互联网 发布:linux telnet连接失败 编辑:程序博客网 时间:2024/04/29 19:52

进入数据库: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"

1 0
原创粉丝点击