=== mongodb的基本操作命令=================
DDL操作===========================================================================================
创建架构:use Schema ; 在Schema中创建一个对象即可 db.createCollection("CollectionName")。相当于 create database DBName ;
创建一个集合: db.createCollection("test"); 相当于DBMS中的创建表 create table TableName();
展示架构: show dbs; 相当于mysql 中的 show databases ;
展示集合个数: show collections; show tables
删除当前架构:use Schema ; db.dropDatabase(); drop database DBName
修改集合名:db.CollectionName.renameCollection("NewName") ; 类似 mysql 中的 alter table oldTable rename to NewTable ;
删除集合:db.collections.drop(); 类似 mysql 中 drop table tableName;
查看当前所在的DB:db
用户相关操作================================================================
添加用户:db.addUser("UserName","PWD")
验证用户是否添加成功:db.auth("UserName","PWD")
删除用户:db.removUsers("testUser")
权限相关:
http://blog.itpub.net/22664653/viewspace-715617/
DML操作=====================================================================
查询集合:db.CollectionName.find({}) ; 相当于 select * from TableName
等式条件查询:db.CollectionName.find({key01:value01}); 相当于 select * from TableName where key01=value01
查询字段的不同值(去重):db.CollectionName.distinct("Key") 类似 select distinct(key) from table
模糊查询:db.collections.find({key:/ang/}); select * from tableName where key like ‘%ang%‘ ;
:db.collections.find({key:/^ang/}) ; select * from tableName where key like ‘ang%‘ ;
:db.collections.find({key:/g$/}); select * from tableName where key like ‘%ang‘
查询出指定的列明:db.collections.find({},{key01:false,key02:true,key03:true}); select key02,key03 from tableName
范围查找:db.collections.find({key01:{$gt:20}}); select * from tableName where key01 > 20
:db.collections.find({key01:{$lge:20}}); select * from tableName where key01 >= 20
:db.collections.find({key01:{$lt:20}}); select * from tableName where key01 < 20
:db.collections.find(){key01:{$lte:20}}; select * from tableName where key01 <= 20
升序排序:db.collections.find({}).sort({key:1}); select * from tableName order by key asc
降序排序:db.collections.find({}).sort({key:-1}); select * from tableName order by key desc
记录汇总:db.collections.find({}).count({}); select count(*) from tableName
:db.collections.find({}).count({key:false}) select count(key) from tableName
返回指定行数:DBQuery.shellBatchSize=num/db.collections.find({}).limit(num) 类似于 sqlserver set rowcount num
or的操作:db.collections.find({"$or":[{key01:valu01},{key02:value02}]}); select * from tableName where key01=valu01 or key02=value02
in操作:db.test.find({key01:{$in:[valu01,valu02,valu03]}}); select * from tableName where key01 in (valu01,valu02,valu03)
and的操作:db.collections.find({key01:{$gte:24,$lte:28}}); select * from tableName where key01 >=24 and key01 <= 28
:db.collections.find({key01:valu02,key02:valu02}); select * from tableName where key01=valu01 and key02=valu02
不等于操作:db.collections.find({key01:{$ne:valu01}}); select * from tableName where key01 <> valu01
$not的相关操作:db.collections.find({key:{$not:{$in:[valu01,valu02]}}}); select * from tableName where key not in (valu01,valu02)
注意:db.collections.find(key:{$not:valu01}); 没有这种写法
正则表达式:后续研究
数组的相关操作
$mod:取模函数
$inc
$maxscan:制定本次查询中扫描文档的最大数量
$min:查询的开始条件,
$max:查询的结束条件
$showDiskLoc:显示该条结果在磁盘上的位置。
删除数据:db.CollectionName.remove({key:value});
修改数据:db.collections.update({key01:valu01}},{"$set":{key02:NewValue}});
添加数据:db.CollectionName.Save({key01:value01,key02:value02})/db.CollectionName.insert({key01:value01,key02:value02})
查看帮助: db.CollectionName.help();