欢迎光临
我们一直在努力

MongoDB使用总结系列2

 由于习惯了使用关系型数据库,觉得SQL语句对数据进行操作的灵活性不用多说,也很好理解和掌握,但是开始用MongoDB后,在客户端命令行中对一些数据进行操作时总是很别扭,总是提示语法错误,尽管RockMongo或MongoVUE等工具 提供了很多便利,但有些操作还是需要命令行操作,于是将在命令行中的数据操作命令做了个大概的总结:

1.查看命令提示

db.help();

db.collname.help();

db.collname.find().help();

re.help();

2.切换创建数据库

use dbname;

3.查询所有数据库

show dbs;

4.删除当前使用的数据库

db.dropDataBase();

5.创建集合(table)

db.createCollection("name",{size:1,capped:5,max:100});

6.查询

db.table.find() 相当于:select * from table

db.table.distinct("name") 相当于:select distinct name from table;

db.table.find({"age":22}) 相当于:select * from table where age=22;

db.table.find({"age":{$gte:22}}) 相当于 select * from table where age>=22;

db.table.find({"age":{$lt:22}}) 相当于 select * from table where age<22;

db.table.find({"age":22},{"name":1}) 相当于 select name from table where age=22

db.table.find({"age":{$gt:10,$lt:30}}) 相当于 select * from table  where age>10 and age<=30

db.table.find({"name":/mary/});相当于 select * from table where name like ‘%mary%’

7.新增

db.table.save({name:1,age:1}); 相当于insert into table (name,age) values (1,1);

8.修改

db.table.update({age:1},{$set:{name:mary}},false,true) 相当于update table set name=mary where age=1

9.删除

db.table.remove({age:1}) 相当于 delete from table where age=1

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。